8. Looping and Lists





1. How Many Words?


word = input('Word: ')
while word != '':
  words.append(word)
  word = input('Word: ')

words.sort()

#print(words)
#print(len(words))

# now count up unique words

num = 0
prev = ""
for word in words:
    if word != prev:
        prev = word
        num = num + 1


print("You know {} unique word(s)!".format(num))



2. One car, two car, red car, blue car!

cars = input("Cars: ")

carArray = cars.split(" ")

blues = 0
reds = 0
for car in carArray:
    if car=="blue":
        blues +=1
    elif car == "red":
        reds += 1

print ("red: {}".format(reds))
print ("blue: {}".format(blues))


3. BaSe fOO ThE AttAcK

code = input("code: ")
codes = code.split(" ")
codes.reverse()
up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#print(codes)
says=""

if len(code.strip())>0:
    for word in codes:
        #print(word[0])
        if word[0] in up:
            says = says + word.lower()+" "

print("says: {}".format(says.rstrip()))

4. Yuor biran is an azamnig thnig

# Super Anagram is a special kind of anagram. A Super Anagram is an anagram whose first and last letters are the same.

words = input("Enter words: ")
wordArray = words.split()  # convert sentence into an array with each word in a separate element

#print(wordArray)
word1A = list(wordArray[0])  #convert each word into an array with each of the letters in a separate element
word2A = list(wordArray[1])

w1a1 = word1A[0]
w1al = word1A[len(word1A)-1]
w2a1 = word2A[0]
w2al = word2A[len(word2A)-1]

word1A.sort()   # sort all the letters into alphabetic order
word2A.sort()

word1 = "".join(word1A)  #join all the letters back together to make a word
word2 = "".join(word2A)

#print(word1,word2)

#if each of the words are anagram and contain the same letters (even if in different order)
# And the first letters and last letters of each word is the same,
# then Super Anagram

if (word1==word2) and (w1a1==w2a1 and w1al==w2al):
    print ("Super Anagram!")
else:
    print ("Huh?")




Comments