Docsity

Python Code Snippets: Solutions for Common Programming Problems, Exams of Computer Science

Toggle sidebar
bg1
D335 |! Python |! Endgame |! Fun-lets |!
Newest |! Exam |! with |! complete |!
solutions
Convert |! pairs |! of |! values |! in |! a |! csv |! row |! into |! a |! dictionary |! - |! Correct |! answer |!
✔for |! row |! in |! csv_reader: |!
|! row_dict |! = |! dict(zip(row[::2], |! row[1::2]))
|! print(row_dict)
Extract |! words |! listed |! alphabetically |! by |! line |! from |! a |! text |! file |! and |! create |! a |!
dictionary |! with |! the |! first |! letter |! of |! the |! words |! comprising |! the |! key, |! and |! the |!
words |! themselves |! in |! a |! list |! as |! the |! corresponding |! value. |! - |! Correct |! answer |!
✔with |! open(<file>, |! 'r') |! as |! f:
|! dic |! = |! {}
|! lines |! = |! f.readlines()
|! for |! line |! in |! lines:
|! dic[line[0]] |! = |! line.split()
Given |! a |! ten-digit |! integer, |! isolate |! the |! first |! three, |! next |! three |! and |! last |! four |!
digits |! using |! floor |! and |! modulo. |! - |! Correct |! answer |! ✔#shear |! right |! 7 |! digits
first_three |! = |! ten_digits |! // |! 10000000
pf3
pf4
pf5

Partial preview of the text

Download Python Code Snippets: Solutions for Common Programming Problems and more Exams Computer Science in PDF only on Docsity!

D335 |! Python |! Endgame |! Fun-lets |!

Newest |! Exam |! with |! complete |!

solutions

Convert |! pairs |! of |! values |! in |! a |! csv |! row |! into |! a |! dictionary |! - |! Correct |! answer |! ✔for |! row |! in |! csv_reader: |! |! row_dict |! = |! dict(zip(row[::2], |! row[1::2])) |! print(row_dict) Extract |! words |! listed |! alphabetically |! by |! line |! from |! a |! text |! file |! and |! create |! a |! dictionary |! with |! the |! first |! letter |! of |! the |! words |! comprising |! the |! key, |! and |! the |! words |! themselves |! in |! a |! list |! as |! the |! corresponding |! value. |! - |! Correct |! answer |! ✔with |! open(, |! 'r') |! as |! f: |! dic |! = |! {} |! lines |! = |! f.readlines() |! for |! line |! in |! lines: |! dic[line[0]] |! = |! line.split() Given |! a |! ten-digit |! integer, |! isolate |! the |! first |! three, |! next |! three |! and |! last |! four |! digits |! using |! floor |! and |! modulo. |! - |! Correct |! 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 Insert |! SSN-style |! dashes |! - |! Correct |! answer |! ✔original_string |! = |! '123456789' formatted_string |! = |! original_string[:3] |! + |! '-' |! + |! original_string[3:5] |! + |! '-' |! + |! original_string[5:] print(formatted_string) Load |! the |! contents |! of |! a |! csv |! file |! into |! memory |! - |! Correct |! answer |! ✔import |! csv with |! open('data.csv', |! 'r') |! as |! f: |! csv_reader |! = |! csv.reader(f) Manipulate |! text |! from |! type() |! method |! - |! Correct |! answer |! ✔my_string |! = |! '' type(my_string).name type_object |! = |! type(my_string)

Print |! a |! Fibonacci |! number |! from |! the |! Fibonacci |! sequence |! up |! to |! the |! input |! integer. |! Assume |! all |! inputs |! > |! 0 |! - |! Correct |! answer |! ✔a, |! b |! = |! 0, |! 1 for |! r |! in |! range(2, |! int(input())): |! a, |! b |! = |! b, |! a |! + |! b print(a |! + |! b) Raise |! exceptions |! for |! searching |! a |! dictionary |! and |! output |! the |! exception |! message. |! Assume |! the |! dictionary |! and |! a |! class |! to |! create |! the |! exception |! message |! object |! are |! already |! provided. |! - |! Correct |! answer |! ✔if |! k |! in |! dic: |! return |! dic[k] else: |! raise |! EntryNotFoundError(f'Value |! not |! found |! for |! {k}') for |! k, |! v |! in |! dic.items(): |! if |! v |! == |! value: |! return |! k |! else: |! raise |! EntryNotFoundError(f'Key |! not |! found |! for |! {v}') try: |! except |! EntryNotFoundError |! as |! excpt: |! print('<optional |! message>: |! ', |! excpt.message)

Replace |! section |! of |! text |! in |! my_file.txt |! - |! Correct |! answer |! ✔file_name |! = |! 'my_file.txt' with |! open(file_name, |! 'r+') |! as |! f: |! content |! = |! f.read() |! content |! = |! content.replace('_photo.jpg','_info.txt') |! f.seek(0) |! f.write(content) |! f.truncate() Sort/clean |! dictionary |! - |! Correct |! answer |! ✔sorted_dic |! = |! {key: |! dic[key] |! for |! key |! in |! sorted(dic)} cleaned_dict |! = |! {key.strip(): |! value.strip() |! for |! key, |! value |! in |! my_dict.items()}