You are on page 1of 4

file=open("school_prompt2.

txt", "r" )
contents=file.read()
print(contents)
num_char=len(contents)
print(num_char)
file.close()

file=open("travel_plans2.txt", "r")
lines=file.readlines()
print(lines)
num_lines=len(lines)
print(num_lines)
file.close()

file=open("emotion_words2.txt", "r")
content=file.read()
first_forty=content[:40]
print(first_forty)

file=open("emotion_words.txt", "r")
count=0
for w in file.readlines():
count+=1
num_lines=count
print(num_lines)
file.close()

olympians = [("John Aalberg", 31, "Cross Country Skiing"),


("Minna Maarit Aalto", 30, "Sailing"),
("Win Valdemar Aaltonen", 54, "Art Competitions"),
("Wakako Abe", 18, "Cycling")]

outfile = open("reduced_olympics.csv", "w")


# output the header row
outfile.write('Name,Age,Sport')
outfile.write('\n')
# output each of the rows:
for olympian in olympians:
row_string = '{},{},{}'.format(olympian[0], olympian[1], olympian[2])
outfile.write(row_string)
outfile.write('\n')
outfile.close()

f = open('scarlet.txt', 'r')
txt = f.read()
# now txt is one long string containing all the characters
letter_counts = {} # start with an empty dictionary
for c in txt:
if c not in letter_counts:
# we have not seen this character before, so initialize a counter for it
letter_counts[c] = 0
#whether we've seen it before or not, increment its counter
letter_counts[c] = letter_counts[c] + 1

for c in letter_counts.keys():
print(c + ": " + str(letter_counts[c]) + " occurrences")

f = open('scarlet2.txt', 'r')
txt = f.read()
# now txt is one long string containing all the characters
x = {} # start with an empty dictionary
for c in txt:
if c not in x:
# we have not seen this character before, so initialize a counter for it
x[c] = 0
#whether we've seen it before or not, increment its counter
x[c] = x[c] + 1
letter_values = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f':4, 'g': 2, 'h':4,
'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1, 's':1,
't':1, 'u':1, 'v':4, 'w':4, 'x':8, 'y':4, 'z':10}
tot = 0
for y in x:
if y in letter_values:
tot = tot + letter_values[y] * x[y]
print(tot)

The dictionary travel contains the number of countries within each continent that
Jackie has traveled to. Find the total number of countries that Jackie has been to,
and save this number to the variable name total. Do not hard code this!
travel = {"North America": 2, "Europe": 8, "South America": 3, "Asia": 4,
"Africa":1, "Antarctica": 0, "Australia": 1}
num=list(travel.values())
total=0
for w in num:
total+=w
print(total)

Create a dictionary called lett_d that keeps track of all of the characters in the
string product and notes how many times each character was seen. Then, find the key
with the highest value in this dictionary and assign that key to max_value.
product = "iphone and android phones"
lett_d={}
for w in product:
if w not in lett_d:
lett_d[w]=0
lett_d[w]+=1
print(lett_d)
max_value=list(lett_d)[0]
for r in lett_d:
if lett_d[r] > lett_d[max_value]:
max_value=r
print(max_value, "has max no. of occurences = ", lett_d[max_value])

Create a dictionary, freq_words, that contains each word in string str1 as the key
and its frequency as the value.
str1 = "I wish I wish with all my heart to fly with dragons in a land apart"
str2=list(str1.split(" "))
print(str2)
freq_words={}
for w in str2:
if w not in freq_words:
freq_words[w]=0
freq_words[w]+=1
print(freq_words)

You might also like