Skip to content

lab07 practice problems#38

Open
noahstier wants to merge 1 commit into
ucsb-cs8:masterfrom
noahstier:Lab07Practice
Open

lab07 practice problems#38
noahstier wants to merge 1 commit into
ucsb-cs8:masterfrom
noahstier:Lab07Practice

Conversation

@noahstier
Copy link
Copy Markdown
Contributor

Sections for lab07 have already started but we could add these anyway and make an announcement about it

Comment thread _lab/lab07.md

# Extra practice

1. Write a function `set_intersection(A, B)` that takes two lists of numbers A and B as inputs, and returns a list containing all numbers common to both input lists. If a number occurs n times in both lists, it should occur n times in the result. The order of the result should be the order of occurrence in the first input list. Examples: `set_intersection([1, 8, 2, 1], [4, 1, 8])` should return `[1, 8]`; `set_intersection([8, 1, 2, 1], [4, 1, 8, 1])` should return `[8, 1, 1]`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first and second problems are good practice exercises. I would call it get_intersection

Comment thread _lab/lab07.md

2. Write a function that concatenates two input files (reads them and combines them into one by putting one after the other) and writes the result to an output file: `concatenate(infile1, infile2, outfile)`.

3. Write a function that takes a dict of filename-word pairs as input, splits each file at each occurrence of its associated word, and writes an output file for each split. `split_files({"animals.txt": "giraffe", "dinosaur.txt": "velociraptor"})` should read "animals.txt", split it up into n+1 chunks where the chunks are bounded by n occurrences of the word "giraffe" (no final chunk should contain the word "giraffe"), and write the chunks to output files "giraffe1.txt", ..., "giraffe\<n\>".txt. Then it should do the same for the dinosaur file. Note: you may want to start by writing a helper function.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing, esp. since it might not be clear to them what the helper function is supposed to do.

Comment thread _lab/lab07.md

3. Write a function that takes a dict of filename-word pairs as input, splits each file at each occurrence of its associated word, and writes an output file for each split. `split_files({"animals.txt": "giraffe", "dinosaur.txt": "velociraptor"})` should read "animals.txt", split it up into n+1 chunks where the chunks are bounded by n occurrences of the word "giraffe" (no final chunk should contain the word "giraffe"), and write the chunks to output files "giraffe1.txt", ..., "giraffe\<n\>".txt. Then it should do the same for the dinosaur file. Note: you may want to start by writing a helper function.

4. Write functions implementing the string methods `split`, `isnumeric`, `replace`, and `join`. For example, `replace("hello world", "l", "_")` should return "he__o world". In a Python interpreter, run `dir("")` to get a list of other string methods that might be good practice candidates.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they supposed to re-implement the available functions?

Comment thread _lab/lab07.md

4. Write functions implementing the string methods `split`, `isnumeric`, `replace`, and `join`. For example, `replace("hello world", "l", "_")` should return "he__o world". In a Python interpreter, run `dir("")` to get a list of other string methods that might be good practice candidates.

5. Write a function that "rotates" a dictionary of string-int pairs by a given shift value: return a dictionary where each value is assigned to the key that is `shift` keys away from its original key, where the ordering of keys is defined by alphabetical sorting. For example: `rotate_dict({"guitar": 4, "violin": 9, "banjo": -3, "cello": 0}, 2)` should return `{"guitar": -3, "violin": 0, "banjo": 9, "cello": 4}`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the real-world example for when to use this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants