Installing Python
Problem
After downloading and installing Python, type import this
into the Python command line and see what happens. Then, click the "Download dataset" button below and copy the Zen of Python into the space provided.
Answer
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those
Variables and Some Arithmetic
Problem
Given: Two positive integers
and , each less than 1000. Return: The integer corresponding to the square of the hypotenuse of the right triangle whose legs have lengths
and . source code
a = 913
b = 899
result = a**2 + b**2
print(f'{a}^2 + {b}^2 = {result}')
answer
1641770Strings and Lists
Problem
Given: A string
of length at most 200 letters and four integers , , and . Return: The slice of this string from indices
through and through (with space in between), inclusively. In other words, we should include elements and in our slice.
Source Code
wordOneStartPos = 18 wordOneEndPos = 28 wordTwoStartPos = 99 wordTwoEndPos = 108 txtStr = "nAdcR5emh1hsdYO8ISRhacophorusJTOfq4KdgBpDJwqVu1tszXAhJ4Vi6G9G9g9qIIIwue07MopaeA5siAZCWVzYorUv4msA5astandingiieEut4kYggXnAoIeYCILZCSfbc9dBBpMXSj7dEaDLxitEk6Sv6Y5AMot2d." print(f'{txtStr[wordOneStartPos:wordOneEndPos + 1]} {txtStr[wordTwoStartPos:wordTwoEndPos + 1]}')AnswerRhacophorus standingiiConditions and Loops
Problem
Given: Two positive integers
and ( ). Return: The sum of all odd integers from
through , inclusively.
Source Code
startPos = 4310 endPos = 8480 result = 0 for x in range(startPos, endPos + 1): if x % 2 != 0: result += x print(result)Answer
13333575Working with Files
Problem
Given: A file containing at most 1000 lines.
Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.
Source Code
i = 1 f = open('input.txt') for line in f.readlines(): if i % 2 == 0 : print (line) i += 1AnswerSome things in life are bad, they can really make you mad Other things just make you swear and curse When you're chewing on life's gristle, don't grumble give a whistle This will help things turn out for the best Always look on the bright side of life Always look on the right side of life If life seems jolly rotten, there's something you've forgotten And that's to laugh and smile and dance and sing When you're feeling in the dumps, don't be silly, chumps Just purse your lips and whistle, that's the thing So, always look on the bright side of death Just before you draw your terminal breath Life's a counterfeit and when you look at it Life's a laugh and death's the joke, it's true You see, it's all a show, keep them laughing as you go Just remember the last laugh is on you Always look on the bright side of life And always look on the right side of life Always look on the bright side of life And always look on the right side of lifeDictionaries
Problem
Given: A string
of length at most 10000 letters. Return: The number of occurrences of each word in
, where words are separated by spaces. Words are case-sensitive, and the lines in the output can be in any order.
Source Code
txtStr = "When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be" wordCoutDict ={} for word in txtStr.split(' '): if word in wordCoutDict: wordCoutDict[word] += 1 else: wordCoutDict[word] = 1 #wordCoutDict = Counter(txtStr.split('')) for key, value in wordCoutDict.items(): print(key, value)AnswerWhen 1 I 2 find 1 myself 1 in 4 times 1 of 11 trouble 1 Mother 2 Mary 2 comes 2 to 3 me 4 Speaking 3 words 7 wisdom 7 let 30 it 36 be 41 And 3 my 1 hour 1 darkness 1 she 1 is 4 standing 1 right 1 front 1 Let 6 Whisper 4 when 2 the 4 broken 1 hearted 1 people 1 living 1 world 1 agree 1 There 4 will 5 an 4 answer 4 For 1 though 1 they 2 may 1 parted 1 there 2 still 2 a 2 chance 1 that 2 see 1 night 1 cloudy 1 light 1 shines 1 on 1 Shine 1 until 1 tomorrow 1 wake 1 up 1 sound 1 music 1 yeah 2
0 Comments