Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #A. Rahman Sadiq
- #Due: Monday, November, 24th, 2014
- #Submitted to ICS3U1-03
- #The input for question 1 is the german sentance, output is the translated english sentance (from mydict)
- #The input for the extra part is mylist, the output is the length of the list (# of words)
- #The input for question 2 and 3 is sentance, the output is the number of times each word appears (for #2) and a new sentance with the words that appear more than once (for #3).
- #The input for question 4 is the english sentance, output is the translated german sentance (from mydict)
- #The input for question 5 and 6 is words (the string), the output is the number of times each word appears according to frequency (largest to smallest order).
- #This is the first part
- #Part 1-3 from word file
- #This is the first part
- myDict = {"befreit":"liberated", "baeche":"brooks", "eise":"ice", "sind":"are", "strom":"river", "und":"and", "vom":"from"}
- mySent = ("vom eise befreit sind strom und baeche") #defines german words as a sentance.
- for x in mySent.split(): #splits german sentance.
- print(myDict[x], end =" ") #From dictionary, prints english substitute for mysent.
- ############################################################################################################################################################
- #This is the first part (hardcode method)
- mysent = dict()
- mysent['vom eise befreit sind strom und baeche'] = 'from ice liberated are river and brooks' #Shows that german version equals to the english translation
- print(mysent)
- ############################################################################################################################################################
- ############################################################################################################################################################
- #This is extra (gets length of string)
- mylist = ["Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day"]
- numberofwords = [len(sentence.split()) for sentence in mylist] #splits sentance into individual words than finds the length (equals to # of words)
- print(numberofwords)
- ############################################################################################################################################################
- #This is the second part
- mySent = ("Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day")
- mySent = mySent.lower() #lowers the sentance
- SentCounter = dict() #makes it into a dict
- for x in mySent.split():
- SentCounter[x] = 0
- for x in mySent.split():
- SentCounter[x] += 1
- print (SentCounter) #prints out the amount of times the word appears in mysent
- #This is the third part
- XDict = dict() #defines XDict as a dict
- for x in SentCounter:
- if SentCounter[x] != 1: #if the number of words that appears in SentCounter is not 1
- XDict[x] = SentCounter[x]
- print (XDict) #prints the series of words that appear more than once
- ############################################################################################################################################################
- #This is the second and third part (using count)
- y=""
- sentance = "Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day"
- lowercase = sentance.lower() #lower cases all words in string.
- words = lowercase.split()
- for word in words:
- print(word, " ",lowercase.count(" "+word+" ")) #prints out the number of times each word appears
- if lowercase.count(" "+word+ " ")!=1:
- y = y+word+" "
- print(y) #prints out the words that appear more than once, defined by the previous 2 lines.
- ############################################################################################################################################################
- #This is the fourth part
- #Part 4-6 from .txt file
- mydict = {"liberated":"befreit", "brooks":"baeche", "ice":"eise", "are":"sind", "river":"strom", "and":"und", "from":"vom"}
- mysent = ("from ice liberated are river and brooks") #Same as number one, mysent is the english sentance and the code splits the english sentance and replaces the words with its german substitute (from mydict), printing the translated german sentance.
- for x in mysent.split():
- print(mydict[x], end =" ")
- #This is the fifth and sixth part
- from collections import Counter
- words = "Banana Banana Banana Banana Banana Banana Banana Apples Apples Apples Apples Apples Strawberries Strawberries Strawberries Strawberries Strawberries Strawberries Eggs Eggs Eggs Eggs"
- freqs = Counter(words.split()) #splits the string into individual words and counts the # of times each word appears in the string, sorts according to frequency (largest to smallest)
- print(freqs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement