CS1101-Unit06 programming assignment
docx
keyboard_arrow_up
School
University of the People *
*We aren’t endorsed by this school
Course
1101
Subject
Aerospace Engineering
Date
Jan 9, 2024
Type
docx
Pages
4
Uploaded by BaronEmuPerson965
a.
1.
I assigned the variable nameslist to a list consisting of 10 names: ['John Smith', 'Jane Doe', 'Charles Brown', 'Christine Johnson', 'Sally Mae', 'Mary Moore', 'Bill Richards', 'Janet Suarez', 'Chris Murphy', 'Jean Martinez']. I split it into sublist1 and sublist2 using the slice operator, specifying sublist1 to include up to the 5th index item and sublist2 to include items after the 5th index.
2.
To add the new name to the end of sublist2 I used the append method, sublist2.append(‘Kriti Brown’)
3.
I deleted the second name of sublist1 by using the del operator, del sublist1[1]
4.
sublist1 and sublist2 were merged together into the new list, merged, using the append operator, merged = sublist1 + sublist2
5.
salarylist is a list consisting of 10 salaries, [10000, 12000, 13000, 14000, 20000, 22000, 25000, 30000, 33000, 35000]. To give each a raise of 4%, it must be multiplied by 1.04 using the
* operator. The new list raisesalaries contains the updated salaries including the 4% increase.
6.
To sort the raisesalaries list from highest to lowest, I used sortsalaries = sorted(raisesalaries, reverse=True) (it is necessary to specify reverse=True
because otherwise it will sort from lowest to highest by default.) The top 3 highest salaries from the list are obtained by slicing, top3 = sortsalaries[:3]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
b.
This is the program I have written:
The user is prompted to enter a sentence, which is then split into a list of words using the split method, wordlist = sentence.split()
. The list is reversed using the reverse() method, wordlist.reverse()
. The words are joined back together to create newsentence through the join function, newsentence = " ".join(wordlist)
(“ “ specifies that the words should be separated by a space between each one.)
The program concludes by printing the original sentence and new sentence. When the program is run, the user sees the following output: