Exercises Week 3 Les 1 - oefeningen week 3 python PDF

Title Exercises Week 3 Les 1 - oefeningen week 3 python
Course Algoritmen en datastructuren
Institution Universiteit Hasselt
Pages 11
File Size 203.5 KB
File Type PDF
Total Downloads 46
Total Views 149

Summary

oefeningen week 3 python...


Description

Exercises Week 3 Les 1 Exercise 5.1. Ex5.1.py def count_words(s): words = 0 for word in s.split(): words += 1 return words print(count_words("Dit is een string.")) OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.1.py 4 PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.2 Ex5.2.py def pretty_print(s, m, n): s = shorten_string(s, n) if m < n: m = n + 4 k = (m // 2 * "*") + s + (m // 2 * "*") print(k)

def shorten_string(s,n): if len(s) > n: return s[:n - 3] + "..." else: return s;

pretty_print("123456", 6, 10) OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.2.py *******123456******* PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.3. Ex5.3.py import string

def remove_punct(s): k = "" for c in s: if c not in string.punctuation and c not in string.whitespace: k += c return k

def reverse(s): reversestring = "" i = len(s) while i > 0: i -= 1 reversestring += s[i] return reversestring

def check_p(s): k = remove_punct(s.lower()) l = reverse(k.lower()) if k == l: print(s, "is een palindroom") else: print(s, "is geen palindroom") check_p("Dit is een test") check_p("koortsmeetsysteemstrook") check_p("A man, a plan, a canal: Panama.") OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.3.py Dit is een test is geen palindroom koortsmeetsysteemstrook is een palindroom A man, a plan, a canal: Panama. is een palindroom PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.4. Ex5.4.py def word_letter_count(s): lc = 0 for c in s: if c != " ": print(c, end="") lc += 1 else: print("", lc) lc = 0 print("", lc, end="") word_letter_count("Dit is een string") OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.4.py Dit 3 is 2 een 3 string 6 PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.5. Ex5.5.py sFile = open("inputText.txt","r") fileContent = sFile.readlines() sFile.close() dFile = open(input("Geef een bestandsnaam in: "),"w") for l in fileContent: dFile.write(l) dFile.close() inputFile.txt: Dit is een text file! Er staat wat tekst in. masterthesis.jpg OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.5.py Geef een bestandsnaam in: OutputFile.txt PS C:\Users\Daan\PycharmProjects\Week3> outputFile.txt: Dit is een text file! Er staat wat tekst in. masterthesis.jpg

Exercise 5.6. Ex5.6.py sFile = open("inputText.txt","r") fileContent = sFile.readlines() sFile.close() dFile = open("outputFile.txt","w") for l in fileContent: dFile.write(l) dFile.write(l) dFile.close() inputFile.txt: Same as in 5.5 OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.6.py PS C:\Users\Daan\PycharmProjects\Week3> outputFile.txt: Dit is een text file! Dit is een text file! Er staat wat tekst in. Er staat wat tekst in. masterthesis.jpg masterthesis.jpg

Exercise 5.7. Ex5.7.py sFile = open(input("Geef een bestandsnaam in: "),"r") words = sFile.read().split() for k in words: for l in words: if k in l and not k == l: print(k, "is een substring van", l) sFile.close() inputText2.txt: dit zijn een paar woorden koortsmeetsysteemstrook koorts meetsysteem strook OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.7.py Geef een bestandsnaam in: inputText2.txt koorts is een substring van koortsmeetsysteemstrook meetsysteem is een substring van koortsmeetsysteemstrook strook is een substring van koortsmeetsysteemstrook PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.8. Ex5.8.py def replace(k, l, m): pos = m.find(k) #Cheating newm = m[:pos] + l + m[pos + len(k):] print(newm) replace("with","for","I play with a sentence without words") OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.8.py I play for a sentence without words PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.9. Ex5.9.py def cleanup_spaces(s): s = s.split() #Cheating k = "" for w in s: k += w + " " return k print(cleanup_spaces("A print(cleanup_spaces("

simple example string Complex example String !

OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.9.py A simple example string ! Complex example String ! PS C:\Users\Daan\PycharmProjects\Week3>

!")) "))

Exercise 5.10. Ex5.10.py #string.upper() import string def toUpper(s): t = "" for c in s: if c in string.ascii_lowercase: t += toUpperChar(c) else: t += c return t def toUpperChar(c): return chr(ord(c) - 32) print(toUpper("DiT iS eEn StRiNg!")) OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.10.py DIT IS EEN STRING! PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.11. Ex5.11.py def encode(s, n): r = "" for c in s: r += chr(ord(c) + n) return r def decode(s, n): r = "" for c in s: r += chr(ord(c) - n) return r s = "Sic transit gloria mundi" e = encode(s, 5) d = decode(e, 5) print("Original:", s) print("Encoded:", e) print("Decoded:", d) OUT: PS C:\Users\Daan\PycharmProjects\Week3> python .\Ex5.11.py Original: Sic transit gloria mundi Encoded: Xnh%ywfsxny%lqtwnf%rzsin Decoded: Sic transit gloria mundi PS C:\Users\Daan\PycharmProjects\Week3>

Exercise 5.13. Ex5.13.a.py sFile = open("netflix.csv","r") movies = [] for l in sFile: movies.append(l.split(", ")) m = 0 #largest n = 0 #index for i in range(len(movies)): if int(movies[i][3]) > m: m = int(movies[i][3]) n = i print(movies[n][0], "has the highest score.") sFile.close() OUT: PS C:\Users\Daan\PycharmProjects\Week3> python.exe .\Ex5.12.a.py Pepe has the highest score. PS C:\Users\Daan\PycharmProjects\Week3> Ex5.13.b.py sFile = open("netflix.csv","r") movies = [] for l in sFile: movies.append(l.split(", ")) m = 0 #total n = 0 #avg for i in range(len(movies)): if movies[i][1] == "Drama": m += 1 n += int(movies[i][3]) print(n / m, "is the average score for Drama movies") sFile.close() OUT: PS C:\Users\Daan\PycharmProjects\Week3> python.exe .\Ex5.12.b.py 83.33333333333333 is the average score for Drama movies PS C:\Users\Daan\PycharmProjects\Week3> Netflix.csv The Birth of a Nation, Drama, 2016, 76, 3-10-2016 The Magnificent Seven, Western, 2016, 54, 3-10-2016 Storks, Fantasy, 2016, 55, 3-10-2016 Snowden, Thriller, 2016, 58, 3-10-2016 Pepe, Drama, 2016, 99, 3-10-2016

Doge, Drama, 2016, 75, 3-10-2016

Excercise 5.14. Ex5.14.py sFile = open("shakespeare.txt","r") #rip ram, 5 MB the = 0 sne = 0 for l in sFile: if "the" in l: the += 1 s = l.split(".") for j in s: sne += 1 print("The entire work of Shakespeare contains the word 'the'", the, "times.") print("It contains", sne, "sentences.") print("Therefore there are, on average,", the / sne, "occurrences of the word 'the' in each sentence.") sFile.close() OUT: PS C:\Users\Daan\PycharmProjects\Week3> python.exe .\Ex5.14.py The entire work of Shakespeare contains the word 'the' 36596 times. It contains 203003 sentences. Therefore there are, on average, 0.18027319793303548 occurrences of the word 'the' in each sentence. PS C:\Users\Daan\PycharmProjects\Week3>...


Similar Free PDFs