Skip to content

Instantly share code, notes, and snippets.

View dazcona's full-sized avatar
🤹‍♂️
Juggling with Data

David Azcona dazcona

🤹‍♂️
Juggling with Data
View GitHub Profile
@dazcona
dazcona / normalise-spaces.py
Created November 7, 2018 14:50
Standard input consists of a single line of text. Write a Python script which copies that line to standard output with leading or trailing spaces removed, and with exactly one space character between words.
#!/usr/bin/env python
t = ""
s = raw_input()
i = 0
while i < len(s):
# Find the position of the next non-space.
while i < len(s) and s[i] == " ": # Linear search.
i = i + 1 #
@dazcona
dazcona / first-number.py
Created November 7, 2018 14:49
Search for the first number in a string
#!/usr/bin/env python
s = raw_input()
# A character ch is a digit if "0" <= ch and ch <= "9".
i = 0 # Linear search.
while i < len(s) and (s[i] < "0" or "9" < s[i]): #
i = i + 1 #
@dazcona
dazcona / first-digit.py
Created November 7, 2018 14:48
Search for the first digit in a string
#!/usr/bin/env python
s = raw_input()
i = 0 # Linear search.
while i < len(s) and (s[i] < "0" or "9" < s[i]): #
i = i + 1 #
if i < len(s):
print s[i]
@dazcona
dazcona / search-non-whitespace.py
Created November 7, 2018 14:48
Search for the position i of the first non-space character in a string
#!/usr/bin/env python
s = raw_input()
i = 0 # Linear search.
while i < len(s) and s[i] == " ": #
i = i + 1 #
print s[i:]
@dazcona
dazcona / snap.py
Created November 7, 2018 14:28
Standard input consists of a sequence of lines of text. Read those lines stopping with the message "snap" when the first adjacent duplicate pair is encountered. The is like the card game snap. You should assume that there is in fact an adjacent duplicate pair in the sequence.
#!/usr/bin/env python
prev = raw_input()
curr = raw_input()
while curr != prev:
prev = curr # Copy the current line to previous for the next iteration.
curr = raw_input() # Read the next line.
print curr
@dazcona
dazcona / dogs.py
Created November 7, 2018 14:26
Counts dogs until end word is found
#!/usr/bin/env python
count = 0
s = raw_input()
while s != "end":
if s == "dog":
count = count + 1
s = raw_input()
@dazcona
dazcona / sum-until-end.py
Created November 7, 2018 13:56
A search loop, print the total sum of the numbers until end is found
total = 0
s = raw_input()
while s != "end":
total = total + int(s)
s = raw_input()
print total
@dazcona
dazcona / dec-to-hex.py
Created November 7, 2018 13:54
Decimal to hexadecimal
# Decimal to hexadecimal
digits = "0123456789abcdef"
base = len(digits) # 16
n = input()
s = ""
while 0 < n:
s = digits[n % base] + s
n = n / base
@dazcona
dazcona / dec-to-bin.py
Created November 7, 2018 13:53
Standard input consists of a single positive (decimal) integer. Write a Python script to convert that number to binary.
digits = "01"
n = input()
s = ""
while 0 < n:
s = digits[n % 2] + s
n = n / 2
print s
@dazcona
dazcona / suffixes.py
Created November 7, 2018 13:18
Read in one line of input (s), output s on one line then all of s except its first character on a second line, then all of s except its first two characters on a third line, and so on
s = raw_input()
i = 0
while i < len(s):
print s[i:]
i = i + 1