0% found this document useful (0 votes)
4 views4 pages

Python Lab

The document contains a series of programming problems for a Python lab, each requiring the implementation of specific functions to process lists or tuples of integers. Each problem includes input and output formats, along with sample inputs and outputs for clarity. The problems cover various topics such as list compression, element categorization, and tuple manipulation.

Uploaded by

rohitsingh100305
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)
4 views4 pages

Python Lab

The document contains a series of programming problems for a Python lab, each requiring the implementation of specific functions to process lists or tuples of integers. Each problem includes input and output formats, along with sample inputs and outputs for clarity. The problems cover various topics such as list compression, element categorization, and tuple manipulation.

Uploaded by

rohitsingh100305
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

Python lab-11

Instructions:
1. Write each program using the function name(s) given in the question.
2. Use a main() function for input, function calling, and output.
3. Call main() using if __name__ == "__main__":.
4. Follow the exact input and output format.
5. Print only the required output.
6. Assume valid input unless stated otherwise.

Q1. Problem:
Read a list of integers. Write a function compress_runs(L) that returns a new list formed by keeping only
the first element from each consecutive block of equal values.
Input Format
• One line containing space-separated integers
Output Format
• Compressed list
Sample Input
555227771
Sample Output
[5, 2, 7, 1]

Q2. Problem:
Read a list of integers. Write a function split_by_sign(L) that returns three lists:
1. all negative numbers in original order
2. all zeros in original order
3. all positive numbers in original order
Then write another function merge_parts(neg, zero, pos) that joins them in the same order: negatives first,
then zeros, then positives.
Print:
1. negative list
2. zero list
3. positive list
4. merged list
Input Format
• One line containing space-separated integers
Output Format
• Line 1: negative list
• Line 2: zero list
• Line 3: positive list
• Line 4: merged list
Sample Input
3 -2 0 5 -7 0 4 -1
Sample Output
[-2, -7, -1]
[0, 0]
[3, 5, 4]
[-2, -7, -1, 0, 0, 3, 5, 4]
Q3. Problem:
Read a list of integers. Write a function first_repeat_info(L) that returns a tuple (value, first_index,
second_index) for the first value whose second occurrence appears earliest while scanning from left to right.
If no element repeats, return None.
Print the tuple if repetition exists. Otherwise print NO REPEAT.
Input Format
• One line containing space-separated integers
Output Format
• Tuple (value, first_index, second_index) or NO REPEAT
Sample Input
4927925
Sample Output
(9, 1, 4)

Q4. Problem:
Read a list of integers. Write a function boundary_sums(L) that returns a new list where:
• the first element remains unchanged
• the last element remains unchanged
• each middle element becomes the sum of its immediate left and right neighbors from the original
list
If the list has fewer than three elements, return it unchanged.
Input Format
• One line containing space-separated integers
Output Format
• Updated list
Sample Input
5 10 20 30 8
Sample Output
[5, 25, 40, 28, 8]

Q5. Problem:
Read a list of integers. Write two functions:
1. unique_in_order(L) that returns a list of values appearing exactly once, preserving original order.
2. repeat_in_order(L) that returns the values appearing more than once, but each such value should
appear only once in the result, preserving the order of first appearance.
Print both lists.
Input Format
• One line containing space-separated integers
Output Format
• Line 1: list of values occurring exactly once
• Line 2: list of repeated values, each printed once
Sample Input
4742975658
Sample Output
[2, 9, 6, 8]
[4, 7, 5]

Q6. Problem:
Read two rows of integers and form a nested list M = [row1, row2]. Write a function columnwise_sum(M)
that returns a list containing column-wise sums. Assume both rows have equal length.
Then write another function extreme_columns(S) that receives the list of sums and returns a tuple
containing:
1. first column sum
2. last column sum
3. difference between last and first column sum
Print both the list and the tuple.
Input Format
• Line 1: first row as space-separated integers
• Line 2: second row as space-separated integers
Output Format
• Line 1: list of column-wise sums
• Line 2: tuple (first_sum, last_sum, difference)
Sample Input
123
456
Sample Output
[5, 7, 9]
(5, 9, 4)

Q7. Problem:
Read integers and store them as a tuple T. Write a function tuple_blocks(T) that returns three tuples:
1. elements at indices divisible by 3
2. elements at indices giving remainder 1 when divided by 3
3. elements at indices giving remainder 2 when divided by 3
Print all three tuples.
Input Format
• One line containing space-separated integers
Output Format
• Line 1: first tuple
• Line 2: second tuple
• Line 3: third tuple
Sample Input
10 20 30 40 50 60 70 80
Sample Output
(10, 40, 70)
(20, 50, 80)
(30, 60)

Q8. Problem:
Read integers and store them as a tuple T. Write a function longest_end_match(T) that returns the maximum
value of k such that the first k elements and the last k elements of the tuple are exactly equal in the same
order.
Print that maximum k.
Input Format
• One line containing space-separated integers
Output Format
• One integer
Sample Input
123412
Sample Output
2
Q9. Problem:
Read a tuple of integers. Write a function center_relation(T) with the following behavior:
• if the length is odd, return a tuple containing:
1. middle element
2. sum of all elements left of middle
3. sum of all elements right of middle
• if the length is even, return a tuple containing:
1. left middle element
2. right middle element
3. sum of all elements in the left half
4. sum of all elements in the right half
Print the returned tuple.
Input Format
• One line containing space-separated integers
Output Format
• Returned tuple
Sample Input 1
5 10 20 30 8
Sample Output 1
(20, 15, 38)
Sample Input 2
479261
Sample Output 2
(9, 2, 20, 9)

Q10. Problem:
Read two integers a and b. Write a function division_report(a, b) that returns a tuple (q, r) where q is the
quotient and r is the remainder.
Then write another function rebuild_number(report, b) that reconstructs the original number using:
dividend = divisor * quotient + remainder
Print:
1. returned tuple
2. reconstructed value
3. CORRECT if reconstructed value equals original a, otherwise WRONG
Assume b != 0.

Input Format
• One line containing two integers
Output Format
• Line 1: tuple (q, r)
• Line 2: reconstructed value
• Line 3: CORRECT or WRONG
Sample Input
17 5
Sample Output
(3, 2)
17
CORRECT

You might also like