D335 Python Endgame Fun-lets
with correct questions and
answers
usr
[COMPANY NAME] [Company address]
, 1. Convert pairs of values in a csv row into a dictionary Answer:
for row in csv_reader Answer: row_dict = dict(zip(row[ Answer: Answer:2],
row[1 Answer: Answer:2]))
print(row_dict)
2. Extract words listed alphabetically by line from a text file and
create a dic- tionary with the first letter of the words comprising the
key, and the words themselves in a list as the corresponding value.
Answer: with open(<file>, 'r') as f Answer:
dic = {}
lines = f.readlines()
for line in lines
Answer:
dic[line[0]] = line.split()
3. Given a ten-digit integer, isolate the first three, next three and last
four digits using floor and modulo. Answer: #shear right 7 digits
first_three = ten_digits // 10000000
#shear right 4 digits then shear left 3 digits
next_three = (ten_digits // 10000) % 1000
#shear left 7 digits
last_four = ten_digits % 10000
4. Insert SSN-style dashes Answer: original_string = '123456789'
formatted_string = original_string[ Answer:3] + '-' + original_string[3 Answer:5] + '-' + original_string[5
Answer:]
print(formatted_string)
5. Load the contents of a csv file into memory Answer: import csv
with open('data.csv', 'r') as f Answer:
csv_reader = csv.reader(f)
6. Manipulate text from
2/
6
with correct questions and
answers
usr
[COMPANY NAME] [Company address]
, 1. Convert pairs of values in a csv row into a dictionary Answer:
for row in csv_reader Answer: row_dict = dict(zip(row[ Answer: Answer:2],
row[1 Answer: Answer:2]))
print(row_dict)
2. Extract words listed alphabetically by line from a text file and
create a dic- tionary with the first letter of the words comprising the
key, and the words themselves in a list as the corresponding value.
Answer: with open(<file>, 'r') as f Answer:
dic = {}
lines = f.readlines()
for line in lines
Answer:
dic[line[0]] = line.split()
3. Given a ten-digit integer, isolate the first three, next three and last
four digits using floor and modulo. Answer: #shear right 7 digits
first_three = ten_digits // 10000000
#shear right 4 digits then shear left 3 digits
next_three = (ten_digits // 10000) % 1000
#shear left 7 digits
last_four = ten_digits % 10000
4. Insert SSN-style dashes Answer: original_string = '123456789'
formatted_string = original_string[ Answer:3] + '-' + original_string[3 Answer:5] + '-' + original_string[5
Answer:]
print(formatted_string)
5. Load the contents of a csv file into memory Answer: import csv
with open('data.csv', 'r') as f Answer:
csv_reader = csv.reader(f)
6. Manipulate text from
2/
6