0% found this document useful (0 votes)
14 views

CS115_Lab02

The document outlines three programming tasks for a lab in CS 115 focused on Python. The first task involves calculating the sum of the series of fractions up to a given integer, the second task requires generating a sequence of integers between two input values, and the third task involves removing adjacent duplicates from user-entered strings until 'exit' is typed. Sample runs illustrate expected inputs and outputs for each task.

Uploaded by

mirzutlawlieya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CS115_Lab02

The document outlines three programming tasks for a lab in CS 115 focused on Python. The first task involves calculating the sum of the series of fractions up to a given integer, the second task requires generating a sequence of integers between two input values, and the third task involves removing adjacent duplicates from user-entered strings until 'exit' is typed. Sample runs illustrate expected inputs and outputs for each task.

Uploaded by

mirzutlawlieya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 115 - Introduction to Programming in Python

Lab 02
Lab Objectives: Strings, Loops, Nested Loops.

Q1: Write a program Lab02_Q1.py to input a positive integer n and displays the sum of the

first n terms of the sequence: In other words, the program should generate the following
sequence: 1 + (1/2) + (1/3) + (1/4) + (1/5) + ... + (1/n)

Assume that the input n is positive. The sum of the fractions should be displayed in 3 digits
after the decimal point.

Sample Run: Sample Run:


Enter an integer (-1 to stop): 1 Enter an integer (-1 to stop): 2
the sum of 1 is 1.000 the sum of 1 + 1/2 is 1.500

Sample Run: Sample Run:


Enter an integer (-1 to stop): 6 Enter an integer (-1 to stop): 10
the sum of 1 + 1/2 + 1/3 + 1/4 + the sum of 1 + 1/2 + 1/3 + 1/4 +
1/5 + 1/6 is 2.450 1/5 + 1/6 + 1/7 + 1/8 + 1/9 +
1/10 is 2.929

Q2: Write a program Lab02_Q2.py to input two integers and print the sequence of integers
between the two input integers, separated by commas and enclosed in square brackets. Display an
increasing sequence if the first argument is smaller than the second; otherwise, display a
decreasing sequence. If the two numbers are the same, that number should be displayed by itself.

Sample Run: Sample Run:


Enter the first integer: 5 Enter the first integer: 17

Enter the second integer: 12 Enter the second integer: 10


[5, 6, 7, 8, 9, 10, 11, 12] [17, 16, 15, 14, 13, 12, 11, 10]

Sample Run:
Enter the first integer: 115

Enter the second integer: 115


[115 ]
Q3: Write a program, Lab02_Q3.py, which prompts the user to enter a string until the user
enters the word 'exit' (not case-sensitive). For each string entered, the program outputs a
modified version of the given phrase by removing adjacent duplicates. The outputs should
appear as in the sample run.

Sample Run:
Enter a string (exit to stop): abccdddd
adjacent duplicates removed: abcd

Enter a string (exit to stop): aa b cccccd eef


adjacent duplicates removed: a b cd ef

Enter a string (exit to stop): good day all!


adjacent duplicates removed: god day al!

Enter a string (exit to stop): aaaaa greattt.


adjacent duplicates removed: a great.

Enter a string (exit to stop): exit


Bye!

You might also like