Exercise 1
Problem Description
We are given a list L
and an integer n
. The task is to repeatedly extend the list L
a certain number of times (n
) by multiplying each element of the list by an increasing factor. The factors start at 2
and increase by 1
for each iteration.
In my solution, I used a while loop to iterate n
times. In each iteration, I used a list comprehension to multiply each element of the list L
by the current factor and then used the extend()
method to add the new elements to the end of the list. Finally, I incremented the factor by 1
and decremented n
by 1
.
Because of the description: L'' == 3 * L', L' == 2 * L
….
My Solution
1 | def f1(L: list, n: int): |
Standard Solution
1 | def f1(L, n): |
Explanation
- My Solution: Uses a while loop where n is manually decremented after each iteration step by step since it can help me understand what we need. I keep track of the current base manually (base += 1).
- Standard Solution: Uses a for loop, which automatically increments i in each iteration. The range is explicitly set to go from 2 to n + 2, which avoids the need for manually handling the base.
Exercise 2
Problem Description
We are given a list L and an integer n. The task is to extend the list L such that it becomes the first n members of an infinite list, which is defined as:
- L, followed by 2 * L, followed by 4 * L, followed by 8 * L, and so on.
This means that:
- L is initially concatenated with 2 * L (all elements of L multiplied by 2).
- Then, 4 * L (all elements of L multiplied by 4) is concatenated, followed by 8 * L, and so on, until the length of the list is at least n.
The challenge is to achieve this without using any for or while loops.
We can see the pattern here: L, 2 * L, 4 * L, 8 * L, ...
until it reaches the length of n.
At first, I ignored the description that “without using any for or while loops.”
Here is my solution:
1 | # Assume that the argument L is a nonempty list of integers |
Standard Solution
1 | def f2(L, n): |
Exercise 3
Problem Description
The goal of this problem is to process a list L, which contains nonempty lists of nonempty lists of integers, and return a pair of lists. Each of these lists will be filtered based on specific conditions related to the length of the sublists and the values inside them.
Here’s the task broken down:
First List in the Pair:
- For each sublist
L'
ofL
, if the length ofL'
is at leastn
, then: - For each sublist
L''
ofL'
, if the sum of the elements ofL''
is strictly positive: - Collect all the strictly positive integers from
L''
and add them to the first list.
- For each sublist
Second List in the Pair:
- This is similar to the first list but slightly simpler:
- For each sublist
L'
ofL
, if its length is at least n: - For each sublist
L''
ofL'
, if the sum of its members is strictly positive: - Directly collect all the strictly positive integers from
L''
and add them to the second list.
The task specifies that we must use list comprehensions only—no loops, functions, or other control structures.
My Solution
1 | def f3(L, n): |
Explanation
First List (
first
):- We first filter out the sublists
L1
whose length is at leastn
(for L1 in L if len(L1) >= n
). - Inside this, for each valid
L1
, we process its membersL2
(which are sublists of integers). We check if the sum of elements of L2 is strictly positive (if sum(L2) > 0
). - For each valid
L2
, we collect the strictly positive integers (for x in L2 if x > 0
). - This gives us the first list of lists, where each list contains the positive integers from sublists of sublists whose sum is positive.
- We first filter out the sublists
Second List (
second
):- Similar to the first list, but here we directly collect the strictly positive integers from the valid sublists
L2
, without additional nesting. - We again check that the sum of
L2
is strictly positive and that the length ofL1
is at leastn
. - The result is a more flattened structure than the first list.
- Similar to the first list, but here we directly collect the strictly positive integers from the valid sublists
Key Concepts:
- Filtering: Both lists involve filtering based on the length of L1 and the sum of L2.
- List Comprehensions: Used extensively to meet the problem’s requirement of not using explicit loops or other control structures.
Standard Solution
1 | def f3(L, n): |
Exercise 4
We are given a list L
of integers, and the goal is to return a list of lists that consists of pairs of elements from the beginning and end of the list. Specifically:
- The number of pairs
n
at the beginning and the end should be as large as possible while maintaining symmetry. - If the length of
L
is odd, the remaining element in the middle (that cannot form a pair) should be included as a single list. - No
for
orwhile
loops should be used in the implementation.
My Solution
1 | def f4(L: list[int]) -> list[list[int]]: |
Standard Solution
1 | def f4(L): |
Explanation
The reason of why we use len(L) // 4 * 2
is that we want to get the number of pairs we can take from both the beginning and end.
We have a pair at the beginning and a pair at the end, so we need to divide the length of the list by 4
to get the number of pairs. We then multiply this by 2
to get the total number of elements in these pairs.
Exercise 5
Before E5, let’s do some exercises.
Normal dictionary:
1 | d = {} |
defaultdict:
1 | from collections import defaultdict |
Counter:
1 | from collections import Counter |
Problem Description
The task is to count the number of customers from different countries by reading a CSV file and then printing the count per country in alphabetical order.
The provided solutions use Python’s csv module to read the data and a dictionary (from the collections.defaultdict class) to keep track of the count of customers from each country.
My Solution
1 | import csv |
Standard Solution
1 | import csv |
Additional Solution
1 | import csv |
Additional Additional Solution (Data Analysis) (NOT Mandatory)
Not Mandatory, but it’s a good way to learn how to use pandas to do data analysis.
1 | import pandas as pd |
Explanation
Reading the CSV file:
1 | df = pd.read_csv(filename) |
Using the read_csv()
function, Pandas can directly load a CSV file into a DataFrame
object, which is similar to a table structure. Each column becomes an attribute of the DataFrame
, and the column names automatically become the labels for the DataFrame
.
Counting the number of customers per country:
1 | df['Country'].value_counts() |
Pandas
provides the value_counts()
method, which can directly count the frequency of each value in a column. Here, we call value_counts()
on the Country
column to get the number of customers from each country.
Exercise 6
Problem Description
The task is to create a directory structure where:
- There is a top-level directory named
First_10_words_per_letter
. - Under this directory, there are two subdirectories:
- Vowels: Containing files for letters
A
,E
,I
,O
,U
,Y
. - Consonants: Containing files for the rest of the letters in the alphabet (B, C, D, etc.).
- Vowels: Containing files for letters
- Each letter (A to Z) has its own file, and each file contains the first 10 words from a dictionary.txt file that start with that letter.
- The directory structure and the files should be generated programmatically, without using any existing hierarchy.
My Solution(Not Perfect, it relies on the sort of dictionary.txt )
1 |
|
Explanation
Explanation of My Solution:
Creating Directories:
- I use
os.makedirs()
to create the top directory (First_10_words_per_letter
) and its subdirectories (Vowels
andConsonants
).
- I use
Reading the Dictionary File:
- The program reads from dictionary.txt and processes each letter in the alphabet (ascii_uppercase).
- For each letter, I decide whether to place the corresponding file in the Vowels or Consonants subdirectory based on whether the letter is a vowel or consonant.
Writing the Words:
- The program writes the first 10 words starting with the given letter into the corresponding .txt file.
Potential Issue:
- here is some limitations of this. Say that processing the dictionary on the fly would not be an option if the words in the file were not lexicographically ordered?
Standard Solution
1 | import os |