jweinst1
10/19/2016 - 11:49 PM

more string practice with decoding

more string practice with decoding

#String + recursion problems

# "\n" is a newline character. It is not represented visually, but makes a new line

#Staircase is a function that returns a successively indented string with a message repeated in every line, it starts with space x up to space y
"""
   print(staircase("TableTennis", 0, 8))
TableTennis
 TableTennis
  TableTennis
   TableTennis
    TableTennis
     TableTennis
      TableTennis
       TableTennis
        TableTennis
"""
def staircase(message, x, y):
	#This creates the spaced string with a certain number of spaces.
	# the join method allows you to concat a list fo strings together.
	spacer = "".join([" " for i in range(x)])
	if x == y:
		return spacer + message
	else:
		return spacer + message + "\n" + staircase(message, x+1, y)
	
#pyramid is a function similar to staircase but uses recursion to synthesize
#the string in the reverse, descending direction as well
"""
   print(pyramid("Berkeley", 8))
Berkeley
 Berkeley
  Berkeley
   Berkeley
    Berkeley
     Berkeley
      Berkeley
       Berkeley
        Berkeley
       Berkeley
      Berkeley
     Berkeley
    Berkeley
   Berkeley
  Berkeley
 Berkeley
Berkeley
"""
def pyramid(message, x):
	def helper(n):
		#way to make a spacer without join method
		spacer = ""
		for i in range(n):
			spacer += " "
		if n == x:
			return spacer + message
		else:
			#you need a newline befor and after the recursive call so the descending string has the same indent
			return spacer + message + "\n" + helper(n+1) + "\n" + spacer + message
	return helper(0)
	
#The atoi function converts a string to an integer without using the int() function
#This does not work for negative integers
#this function also allows a leading zero
"""
   atoi("556")
=> 556
   atoi("5566")
=> 5566
   atoi("55668888")
=> 55668888
   atoi("556068888")
=> 556068888
"""
def atoi(string):
	#A dictionary that gets the integer equivalent for one character of a string
	digits = {
		"0":0,
		"1":1,
		"2":2,
		"3":3,
		"4":4,
		"5":5,
		"6":6,
		"7":7,
		"8":8,
		"9":9
	}
	if not string[0] in digits:
		raise ValueError("Invalid Integer String")
	else:
		if len(string) == 1:
			return digits[string]
		else:
			#the decimal is the magnitude of the number. This grows exponentially with the length of the string.
			decimal = 10 ** (len(string) - 1)
			return (digits[string[0]] * decimal) + atoi(string[1:]) 
			
#splitstr is a function that splits a string by some occurence of a substr within a string.
#it returns a list of strings without the substr you want to split by 
"""
   splitstr("abfcdefg", "f")
=> ['ab', 'cde', 'g']
   splitstr("how are you today sir", " ")
=> ['how', 'are', 'you', 'today', 'sir']
"""
def splitstr(string, substr):
	#the current section of string before a substr occurs
	current_split = ""
	#the strings that are in fact between occurences of the substr
	splits = []
	#tells us whether or not we are in the state of checking for a substr
	splitmode = False
	#keeps track of how far we on checking if a substr has occurred in the string
	subindex = 0
	for i in range(len(string)):
		if splitmode:
			#if we have successfully confirmed the occurence of a substr, perform the split
			if subindex == len(substr):
				#turns off checking for substr
				splitmode = False
				#append the section fo string before the substr
				splits.append(current_split)
				#reset the string carrying the string before the substr
				current_split = ""
				#add the current character to the beginning of the next split
				current_split += string[i]
				subindex = 0
			elif string[i] == substr[subindex]:
				subindex += 1
			else:
				#if the substr is not fully verified via all its characters
				splitmode = False
				#because this is an improper split, add the previously untracked characters to the main split
				current_split += string[i-subindex:i]
				subindex = 0
		else:
			#turn on splitmode if the first char of the substr
			#matches the current char of the string
			if string[i] == substr[subindex]:
				splitmode = True
				subindex += 1
			else:
				current_split += string[i]
	splits.append(current_split)
	return splits