Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_duplicates(my_str):
- # Checks each letter in the string
- for i in range(len(my_str)):
- char = my_str[i]
- count = 1
- # References that letter with every other letter in the string. If they
- # match, the count goes up 1
- for n in range(i + 1, len(my_str)):
- if my_str[n] == char:
- count += 1
- # Checking to see if the letter appeared earlier in the word. If it
- # has, count is set to 0
- for g in range(0, i):
- if my_str[g] == char:
- count = 0
- # If it occurs more than once, print:
- if count >= 2:
- print("{}, Count: {}".format(char, count))
- def string_sum(my_str):
- sum = 0
- for i in range(len(my_str)):
- sum += int(my_str[i])
- return sum
- def translate_phone(phone_str):
- phone_str = list(phone_str)
- new_str = ""
- for i in range(len(phone_str)):
- if phone_str[i].isalpha() == True:
- if phone_str[i] == "A" or phone_str[i] == "B" or phone_str[i] == "C":
- new_str = new_str + "2"
- elif phone_str[i] == "D" or phone_str[i] == "E" or phone_str[i] == "F":
- new_str = new_str + "3"
- elif phone_str[i] == "G" or phone_str[i] == "H" or phone_str[i] == "I":
- new_str = new_str + "4"
- elif phone_str[i] == "J" or phone_str[i] == "K" or phone_str[i] == "L":
- new_str = new_str + "5"
- elif phone_str[i] == "M" or phone_str[i] == "N" or phone_str[i] == "O":
- new_str = new_str + "6"
- elif phone_str[i] == "P" or phone_str[i] == "Q" or phone_str[i] == "R" or phone_str[i] == "S":
- new_str = new_str + "7"
- elif phone_str[i] == "T" or phone_str[i] == "U" or phone_str[i] == "V":
- new_str = new_str + "8"
- elif phone_str[i] == "W" or phone_str[i] == "X" or phone_str[i] == "Y" or phone_str[i] == "Z":
- new_str = new_str + "9"
- else:
- new_str += phone_str[i]
- return new_str
- def add_spaces(my_str):
- new_str = my_str[0]
- for i in range(1, len(my_str)):
- if my_str[i].islower() == True:
- new_str = new_str + my_str[i]
- elif my_str[i].isupper() == True:
- new_str = new_str + " " + my_str[i].lower()
- return new_str
- def get_strings():
- """
- -------------------------------------------------------
- makes a list from strings
- Use: get_strings()
- -------------------------------------------------------
- Postconditions:
- returns:
- list - list of user inputs(list)
- -------------------------------------------------------
- """
- list = []
- x = (input("Enter a new item: "))
- while x != "":
- list.append(x)
- x = (input("Enter a new item: "))
- return list
- def is_word_chain(my_list):
- wordchain = True
- keep_going = True
- for i in range(len(my_list) - 1):
- word = str(my_list[i])
- next_word = str(my_list[i + 1])
- if word[len(word) - 1] != next_word[0]:
- wordchain = False
- return wordchain
- from a8_functions import print_duplicates
- my_str = input("Enter a string: ")
- print_duplicates(my_str)
- from a8_functions import string_sum
- my_str = input("Enter a string of numbers: ")
- result = string_sum(my_str)
- print(result)
- from a8_functions import translate_phone
- phone_str = input("Enter a phone number: ")
- number = translate_phone(phone_str)
- print(number)
- from a8_functions import add_spaces
- my_str = input("Enter a sentance with no spaces: ")
- result = add_spaces(my_str)
- print(result)
- from a8_functions import get_strings, is_word_chain
- my_list = get_strings()
- print(my_list)
- wordchain = is_word_chain(my_list)
- if wordchain == True:
- print("It is")
- elif wordchain == False:
- print("It aint")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement