0% found this document useful (0 votes)
11 views3 pages

CS1101 Learning Journal W6

week 6 learning journal

Uploaded by

brittisaacs19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

CS1101 Learning Journal W6

week 6 learning journal

Uploaded by

brittisaacs19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PART (A)

Explanation:
#Employee's names list:
employee_names is a list containing the names of employees.

#Split employee's list into two sub-lists:


subList1 and subList2 are created by slicing the employee_names list. (Downey, 2015, p. 73–74)

#Add a new employee to subList2:


'Kriti Brown' is appended to subList2. (Downey, 2015, p. 93)

#Remove the second employee's name from subList1:


The second element (index 1) is removed from subList1 using del. (Downey, 2015, p. 94)

#Merge both lists:


merged_list is created by concatenating subList1 and subList2.

#Assume there is another list 'salaryList' storing salaries:


salaryList is created containing salary values.

#Create raise_salaryList with elements of salaryList * 4%:


raise_salaryList is created using a list comprehension, applying a 4% raise to each element
within raise_salaryList.
#Create updated_salaryList by adding salaryList elements to raise_salaryList elements:
updated_salaryList is created by adding each corresponding element from salaryList and
raise_salaryList using “zip”. (Downey, 2015, p. 118)

#Sort the updated_salaryList and show top 3 salaries:


sorted_salaries is a sorted version of updated_salaryList, and top_3_salaries contains the top 3
salaries using ‘sorted’ coupled with ‘reverse=True’ and slicing to sort from top to bottom.

#Output:
The final section prints various results, including the sublists, merged list, raised salary list,
updated salary list, and the top 3 salaries.

PART (B)

Explanation:

#Input sentence from the user:


sentence is a variable that stores the input obtained from the user using the input() function. The
user is prompted to enter a sentence.

#Convert the sentence into a list of words:


The split() method is used on the sentence variable to break it into a list of words. Using split()
separates the input string based on whitespace.
#Reverse the list of words and store it in a new variable:
The reversed() function is applied to the words list to reverse the order of its elements. The
result is a reverse iterator, and list() is used to convert it back to a list. The reversed list is stored
in the variable reversed_words.

#Print the original word list:


The print() function is used to display the original list of words stored in the words variable. This
line outputs "Sentence Word List:" followed by the original list.

#Print the reversed word list:


The print() function is used again to display the reversed list of words stored in the
reversed_words variable. This line outputs "Reversed Word List:" followed by the reversed list.

References: Author, A. D. (2015). Think Python

How to Think Like a Computer Scientist (2nd ed.) Allen Downey.

https://greenteapress.com/thinkpython2/thinkpython2.pdf

W3schools: Python reverse() Function:

https://www.w3schools.com/python/ref_func_reversed.asp

You might also like