Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. def print_duplicates(my_str):
  2. # Checks each letter in the string
  3. for i in range(len(my_str)):
  4. char = my_str[i]
  5. count = 1
  6.  
  7. # References that letter with every other letter in the string. If they
  8. # match, the count goes up 1
  9. for n in range(i + 1, len(my_str)):
  10. if my_str[n] == char:
  11. count += 1
  12.  
  13. # Checking to see if the letter appeared earlier in the word. If it
  14. # has, count is set to 0
  15. for g in range(0, i):
  16. if my_str[g] == char:
  17. count = 0
  18.  
  19. # If it occurs more than once, print:
  20. if count >= 2:
  21. print("{}, Count: {}".format(char, count))
  22.  
  23.  
  24. def string_sum(my_str):
  25. sum = 0
  26. for i in range(len(my_str)):
  27. sum += int(my_str[i])
  28. return sum
  29.  
  30.  
  31. def translate_phone(phone_str):
  32. phone_str = list(phone_str)
  33. new_str = ""
  34. for i in range(len(phone_str)):
  35. if phone_str[i].isalpha() == True:
  36. if phone_str[i] == "A" or phone_str[i] == "B" or phone_str[i] == "C":
  37. new_str = new_str + "2"
  38. elif phone_str[i] == "D" or phone_str[i] == "E" or phone_str[i] == "F":
  39. new_str = new_str + "3"
  40. elif phone_str[i] == "G" or phone_str[i] == "H" or phone_str[i] == "I":
  41. new_str = new_str + "4"
  42. elif phone_str[i] == "J" or phone_str[i] == "K" or phone_str[i] == "L":
  43. new_str = new_str + "5"
  44. elif phone_str[i] == "M" or phone_str[i] == "N" or phone_str[i] == "O":
  45. new_str = new_str + "6"
  46. elif phone_str[i] == "P" or phone_str[i] == "Q" or phone_str[i] == "R" or phone_str[i] == "S":
  47. new_str = new_str + "7"
  48. elif phone_str[i] == "T" or phone_str[i] == "U" or phone_str[i] == "V":
  49. new_str = new_str + "8"
  50. elif phone_str[i] == "W" or phone_str[i] == "X" or phone_str[i] == "Y" or phone_str[i] == "Z":
  51. new_str = new_str + "9"
  52. else:
  53. new_str += phone_str[i]
  54.  
  55. return new_str
  56.  
  57.  
  58. def add_spaces(my_str):
  59. new_str = my_str[0]
  60. for i in range(1, len(my_str)):
  61. if my_str[i].islower() == True:
  62. new_str = new_str + my_str[i]
  63. elif my_str[i].isupper() == True:
  64. new_str = new_str + " " + my_str[i].lower()
  65. return new_str
  66.  
  67.  
  68. def get_strings():
  69. """
  70. -------------------------------------------------------
  71. makes a list from strings
  72. Use: get_strings()
  73. -------------------------------------------------------
  74. Postconditions:
  75. returns:
  76. list - list of user inputs(list)
  77. -------------------------------------------------------
  78. """
  79. list = []
  80. x = (input("Enter a new item: "))
  81. while x != "":
  82. list.append(x)
  83. x = (input("Enter a new item: "))
  84. return list
  85.  
  86.  
  87. def is_word_chain(my_list):
  88. wordchain = True
  89. keep_going = True
  90. for i in range(len(my_list) - 1):
  91. word = str(my_list[i])
  92. next_word = str(my_list[i + 1])
  93. if word[len(word) - 1] != next_word[0]:
  94. wordchain = False
  95.  
  96. return wordchain
  97.  
  98. from a8_functions import print_duplicates
  99.  
  100. my_str = input("Enter a string: ")
  101. print_duplicates(my_str)
  102.  
  103. from a8_functions import string_sum
  104. my_str = input("Enter a string of numbers: ")
  105. result = string_sum(my_str)
  106. print(result)
  107. from a8_functions import translate_phone
  108.  
  109. phone_str = input("Enter a phone number: ")
  110. number = translate_phone(phone_str)
  111. print(number)
  112.  
  113. from a8_functions import add_spaces
  114. my_str = input("Enter a sentance with no spaces: ")
  115. result = add_spaces(my_str)
  116. print(result)
  117.  
  118. from a8_functions import get_strings, is_word_chain
  119. my_list = get_strings()
  120. print(my_list)
  121. wordchain = is_word_chain(my_list)
  122. if wordchain == True:
  123. print("It is")
  124. elif wordchain == False:
  125. print("It aint")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement