0% found this document useful (0 votes)
6 views7 pages

COMP1010 Lab07 1

Uploaded by

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

COMP1010 Lab07 1

Uploaded by

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

College of Engineering and Computer Science

COMP1010 Introduction to Programming


Week 07, Oct 31 2024

Lab 07: Lists, iteration, and loops

Submission Instructions:
• Students must submit Lab Practice 07 on
Canvas and Codeforces with the correct
file names during the lab session.

• The problem for this Lab is available on


Codeforces at this link.

• For each problem, provide clear and con-


cise documentation and comments that thor-
oughly explain your code.

• Students are not required to verify the input


validation for every problem.

• Github repository link.

COMP1010 – Fall 2024 1/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Problem 1: Star Pyramid


Description:
An n-row star pyramid is a solid triangle which has n rows and contains asterisks (*). For example, 5-row
hollow pyramid can be display as:

*
***
*****
*******
*********

Write a Python program that takes an integer n as input and draws the n-row hollow pyramid.

Input Format:
Player list: A single integer n which satisfies 3 ≤ n ≤ 100.

Expected Output:
A solid star pyramid composed of n rows, with each row containing an increasing number of asterisks.

Example:

Test case 1
Input:
3

Output:
*
***
*****

Test case 2
Input:
6

Output:
*
***
*****
*******
*********
***********

COMP1010 – Fall 2024 2/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Problem 2: Finding the Smallest Missing Positive Integer


Description:
Given a list of integers, determine the smallest positive integer that is not present in the list. Write a
python program to solve this problem in two different ways: for loop and while loop.

Input Format:
A list of positive integers, separated by space, having length from 1 to 100. Each integer has ranges from
−105 to 105 .

Part (a): Using a for loop


Write a function smallest integer for that takes the given list as input and returns the smallest missing
positive integer. This function must use the for loop to solve the problem.

Part (b): Using a while loop


Write a function smallest integer while that takes the given list as input and returns the smallest
missing positive integer. This function must use the while loop to solve the problem.

This way shows while loop’s strengths in handling conditions that are not tied to the length of the list.

Expected Output:
Output the smallest positive integer missing from the list using two lines: the first line employs a for
loop, while the second utilizes a while loop.

Example:

Test case 1
Input:
3 4 -1 1

Output:
2
2

Test case 2
Input:
10 9 8 7 6 5 4 3 2 1

Output:
11
11

COMP1010 – Fall 2024 3/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Problem 3: Count the Valleys


Description:
You are given a string where each character signifies either a step up (U) or a step down (D) from a flat
baseline known as sea level. The task is to count the number of distinct valleys formed during the path,
where a valley is a sequence of steps starting with a step down from sea level and ends when you
step back up to sea level.

Given the path DDUUDDUDUUUD.


• The path starts at sea level.
• The first DD means the path goes down two steps below sea level, starting a valley.
• The subsequent UU brings the hiker back to sea level, thus completing one valley.
• The next DD starts another descent but the following U brings it up only one step, so it’s not yet
back to sea level.
• Another D takes the hiker further down again.
• The final UUUD brings the path up to sea level and then above, completing the second valley during
the UU part.

Input Format:
The input has a single string where each character represents a step and can be either U (up) or D (down).
This string’s length ranges from 3 to 104 .

Expected Output:
An integer representing the number of valleys traversed in the given string.

Example:

Test Case 1:
Input:
UDDDUUUD

Output:
1

COMP1010 – Fall 2024 4/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Test Case 2:
Input:
DUDDDUUDDUUUDD

Output:
2

Test Case 3:
Input:
UDDDUUUUUUDDDUDDD

Output:
1

COMP1010 – Fall 2024 5/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Problem 4: Student Ranking


Description:
You are tasked with developing a system to dynamically manage and sort a list of players based on their
scores for a tournament. The system must efficiently handle adding new players, removing players, and
adjusting existing scores while keeping the list organized by descending score. If scores are tied, players
should be sorted alphabetically by name. Your challenge is to write a Python program that implements
this dynamic player management system.

We have 3 specific operations in this system:

1. Insert a Player: Add a new player along with their score to the system. The operation is formatted
as insert PlayerName Score within the operations string.
2. Remove a Player: Remove a player from the system by their name. The operation is formatted
as remove PlayerName within the operations string.
3. Adjust a Player’s Score: Modify the score of an existing player, either increasing or decreasing
it. The operation is formatted as adjust PlayerName ScoreChange within the operations string.

Input Format:
Player list: The first line contains a single string including players and their scores in the format
PlayerName Score, separated by commas.

Operation list: The second line contains a single string including operations, where each operation is
represented in the format operation operation PlayerName [Score or Nothing], separated by com-
mas.

Note: The score of both the input and in each operation ranges from −1000 to 1000.

Part (a): Convert Input into Nested List


Write a function convert input to convert the initial input into a nested list, where each component of
the list is a two-element list containing the player’s name and score with format [Player, Score].

Part (b): Perform Operations


Write a function perform operation to execute the operations on the player list: adding new players,
removing players, and adjusting scores. This function returns the final list after completing all operations.

Part (c): Sort the List


Write a function sort player that takes the final list as input and sort it by score in descending order.
If two players have the same score, sort them alphabetically by their names.

Expected Output:
Print the list of players (name only) sorted from highest to lowest score. For players with the same score,
print them in alphabetical order by name.

COMP1010 – Fall 2024 6/7


College of Engineering and Computer Science
COMP1010 Introduction to Programming
Week 07, Oct 31 2024

Example:

Test case 1
Input:
Thinh 1500, Anh 1200, Tuan 1300
insert Wong 1400, remove Anh, adjust Wong -200

Output:
Thinh Tuan Wong

Test case 2
Input:
Phuc 1500, Vinh 1500, Mien 1500
insert Thinh 1400, adjust Phuc -100, adjust Vinh -100, adjust Mien -100

Output:
Mien Phuc Thinh Vinh

COMP1010 – Fall 2024 7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy