Advertisement
Adehumble

Week5 Coding Exercise 1

Feb 27th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. #1
  2. print("METHOD 1: Using For-Loop")
  3. #Funtion Program
  4. def right_words(expected_list, expected_no):
  5.     answer=[]
  6.     for words in expected_list:
  7.         if len(words)==expected_no:
  8.             answer.append(words)
  9.     print(answer)
  10.  
  11.  
  12. #Main Program  
  13. #The following lines of codes will accepts a list of words
  14. user_list=[]
  15. while True:
  16.     try:
  17.         num=int(input("How many words would you like to play with? "))
  18.         break
  19.     except ValueError:
  20.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  21.         print("\n")
  22.        
  23. for i in range(num):
  24.     word=input("Enter those words of your choice: ")
  25.     user_list.append(word)
  26.  
  27. #The following lines of codes will accepts a specific number i.e. word length
  28. print("\n")
  29. while True:
  30.     try:
  31.         n=int(input("What is the size of the length of words that you want? "))
  32.         break
  33.     except ValueError:
  34.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  35.         print("\n")
  36.  
  37. print("The words with",n,"characters are: ")
  38. right_words(user_list,n)
  39.    
  40. print("\n")
  41.  
  42. print("METHOD 2: Using Filter Function")
  43. #Function Program
  44. def right_words(expected_list):
  45.     return len(expected_list)==n
  46.  
  47. #Main Program
  48. user_list=[]
  49. while True:
  50.     try:
  51.         num=int(input("How many words would you like to play with? "))
  52.         break
  53.     except ValueError:
  54.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  55. print("\n")
  56.        
  57. for i in range(num):
  58.     word=input("Enter those words of your choice: ")
  59.     user_list.append(word)
  60. print("\n")
  61. n=int(input("What is the size of the length of words that you want? "))
  62.  
  63. print("The words with",n,"characters are: ")
  64. print(list(filter(right_words, user_list)))
  65.  
  66.  
  67. print("\n")
  68. print("METHOD 3: Using Lamda Function")
  69. user_list=[]
  70. while True:
  71.     try:
  72.         num=int(input("How many words would you like to play with? "))
  73.         break
  74.     except ValueError:
  75.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  76. print("\n")
  77.        
  78. for i in range(num):
  79.     word=input("Enter those words of your choice: ")
  80.     user_list.append(word)
  81.  
  82. print("\n")
  83. while True:
  84.     try:
  85.         n=int(input("What is the size of the length of words that you want? "))
  86.         break
  87.     except ValueError:
  88.         print("That's a wrong input!\nYou must provide a positive integer. Please, try again.")
  89. print("\n")
  90.  
  91. print("The words with",n,"characters are: ")   
  92. print(list(filter(lambda user: len(user)==n, user_list)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement