[go: up one dir, main page]

0% found this document useful (0 votes)
15 views11 pages

The Joy of Computing Using Python - Unit 10 - Week 7

Uploaded by

kalyanacharjya2
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)
15 views11 pages

The Joy of Computing Using Python - Unit 10 - Week 7

Uploaded by

kalyanacharjya2
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/ 11

9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

(https://swayam.gov.in) (https://swayam.gov.in/nc_details/NPTEL)

abhishekdas26102002@gmail.com 

NPTEL (https://swayam.gov.in/explorer?ncCode=NPTEL) » The Joy of Computing using Python (course)

If already
registered, click
to check your
Week 7 : Assignment 7
payment status Your last recorded submission was on 2025-09-10, 23:15 IST Due date: 2025-09-10, 23:59 IST.
Refer to the code discussed in the "Joy of Computing using Python" lecture on implementing the Snake
and Ladder game.

Course
Ankan and Dilrez are playing a classic game of Snake and Ladder –but this time, it’s implemented in
outline
Python! The game logic is based on the version you’ve seen in the lecture, where snakes pull you down
and ladders push you up. The game board ranges from position 1 to 100.
About NPTEL
()
The snake and ladder positions are stored in dictionaries. For instance:
snakes = {16: 6, 98: 78}
How does an
ladders = {1: 38, 80: 100}
NPTEL online
course work?
Each player begins at position 0. In each turn, a player rolls a die (using random.randint(1, 6)) and
()
moves forward. If they land on the bottom of a ladder, they climb up; if they land on a snake’s mouth,
they slide down. The game alternates between Ankan and Dilrez using a turn variable, and ends when
Week 1 ()
any player reaches exactly 100.

Week 2 ()
Two key functions, check_ladder(position) and check_snake(position), determine whether the player hits
a ladder or a snake after moving.
Week 3 ()

A function reached_end(position) is used to check if a player has reached position 100. A separate
week 4 ()
function is also written to open and display an image of the board using the PIL library.

Week 5 () 1) In the function play(), what is the significance of the variable turn? 1 point

Week 6 () It tracks the number of dice rolls


It ensures alternating turns between the two players
Week 7 () It ends the game after a fixed number of turns
It stores the winning score

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 1/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

Snakes and 2) What happens in the code when a player rolls a number that moves them past position 1 point
Ladders - Not on 100?
the Board (unit?
unit=143&lesson The game ends and they win
=144) The player’s position resets to 0
Snakes and The player stays in the same position (doesn’t move)
Ladders - Not on The player is forced to roll again
the Board - Part
01 (unit?
3) What is the purpose of the following lines in the play() function? 1 point
unit=143&lesson
=145)

Snakes and
Ladders - Not on
the Board - Part
02 (unit?
unit=143&lesson
=146)

Snakes and
To restart the game
Ladders - Not on
the Board - Part To display final scores and exit if the player chooses not to continue
03 (unit? To check if the player reached a snake
unit=143&lesson
=147)
To decide the winner

Snakes and 4) If Ankan is at position 47 and the snakes dictionary contains 47:26, what will happen 1 point
Ladders - Not on
immediately after his dice roll moves him to 47?
the Board - Part
04 (unit?
He will stay at 47
unit=143&lesson
=148) He will move to 100
He will go to 26
Snakes and
Ladders - Not on The game will end
the Board - Part
05 (unit? 5) What does the function show_board() attempt to do? 1 point
unit=143&lesson
=149) Display current scores

Snakes and
Roll the dice for both players
Ladders - Not on Open and display the game board image
the Board - Part
Initialize the snake and ladder dictionaries
06 (unit?
unit=143&lesson
=150)
6) Which of these functions is called after a player moves and before their position is printed? 1 point

Spiral Traversing show_board()


- Let's Animate play()
(unit?
unit=143&lesson
check_ladder() and check_snake()
=151) reached_end() only

Spiral Traversing
7) How many times is the line random.randint(1, 6) evaluated in one full round of both players' 1 point
- Let's Animate -
Part 01 (unit? turns?

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 2/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

unit=143&lesson Once
=152)
Twice
Spiral Traversing
Four times
- Let's Animate -
Part 02 (unit? Depends on the player positions
unit=143&lesson
8) Which part of the code ensures that the game stops immediately when a player reaches 1 point
=153)
exactly 100?
Spiral Traversing
- Let's Animate - if pp1 > end:
Part 03 (unit? if pp1 == 100:
unit=143&lesson
if reached_end(pp1):
=154)
if check_ladder(pp1):
Spiral Traversing
- Let's Animate -
9) Consider the following ladders dictionary (in the format bottom: top): ladders = {1: 38, 4: 14, 1 point
Part 04 (unit?
9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
unit=143&lesson
=155)
If a player is on 27, rolls a 1, and lands on 28, what will happen next?
Spiral Traversing
- Let's Animate - The player will stay on 28 because it's not a ladder base
Part 05 (unit?
The player climbs the ladder to 84
unit=143&lesson
=156) The player must roll again because 28 is a special position
The code throws an error because 28 is not handled explicitly
Spiral Traversing
- Let's Animate -
Part 06 (unit? 10) 1 point
unit=143&lesson
=157)

Spiral Traversing
- Let's Animate -
Part 07 (unit?
unit=143&lesson
=158) Why is the condition turn % 2 == 0 used in this part of the code?

GPS - Track the To check if the game has reached the second round
route (unit?
unit=143&lesson
To make sure player 2 only plays after player 1 wins
=159) To alternate turns between the two players using the value of turn

GPS - Track the To skip the dice roll for even-numbered turns
route - Part 01
(unit?
Dilrez, the ever-curious adventurer, had a habit of collecting strange and shiny things during her
unit=143&lesson
journeys.
=160)

GPS - Track the One day, the village elder gifted her two magical containers — one called a Backpack and the other a
route - Part 02 Pouch.
(unit?
unit=143&lesson
The elder explained:
=161)

GPS - Track the “Dilrez, these are no ordinary containers.


route - Part 03

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 3/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

(unit?
unit=143&lesson The Backpack holds items in a fixed order, and once packed, nothing can be changed.
=162)

GPS - Track the The Pouch, on the other hand, only keeps unique items and doesn’t care about order.”
route - Part 04
(unit? Dilrez was intrigued.
unit=143&lesson
=163) Backpack = Tuple
Week 7
Feedback Form: Dilrez packed it like this:
The Joy of
Computing using backpack = ("map", "compass", "torch")
Python (unit?
unit=143&lesson He checked backpack[0] → 'map'.
=164)

Quiz: Week 7 : But when he tried:


Assignment 7
(assessment? backpack[0] = "magic map"
name=603)
He got an error!
Week 7 :
Programming
Assignment 1 Tuples are ordered, immutable, and allow duplicates.
(/noc25_cs103/p Syntax: () (round brackets)
rogassignment?
name=584) Pouch = Set
Week 7 :
Programming Dilrez filled his pouch:
Assignment 2
(/noc25_cs103/p pouch = {"coin", "potion", "coin"}
rogassignment?
name=585) He expected 3 items, but got:
Week 7 :
Programming {'coin', 'potion'}
Assignment 3
(/noc25_cs103/p He added and removed items freely:
rogassignment?
name=602) pouch.add("gem")

Week 8 ()
pouch.remove("coin")

Text
Sets are unordered, mutable, and store unique items.
Transcripts ()
Syntax: {} (curly braces)

Download 11) What will be the output of the following code? 1 point
Videos ()

Problem
Solving

"map"

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 4/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

Session - July "torch"


2025 ()
"compass"
IndexError

12) What will happen when Dilrez runs this code? 1 point

Prints ("magic map", "compass", "torch")


Gives a TypeError
Prints original backpack
Gives a SyntaxError

13) What will be the output? 1 point

Select all that apply:

The output will contain only unique elements


The output may appear in any order
It will raise a syntax error due to duplicate elements
The output will be a list with 2 elements

14) What will be the final contents of the pouch? 1 point

{'potion', 'gem'}
{'gem'}
['gem']
Error due to remove()

15) What is the type of x in the following code? 1 point

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 5/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

< class 'tuple' >


< class 'list' >
< class 'str' >
< class 'set' >

16) Which of the following statements is true? 1 point

Tuples can’t store duplicate items


Sets maintain the order of elements
Sets automatically remove duplicate items
Tuples can be changed after creation

17) What will this code output? 1 point

33
22
32
23

18) Dilrez tries to update a tuple. Find and fix the error: 1 point

What's the issue?

Tuple index out of range


Syntax error in tuple
Tuples are immutable
Need to use append()

19) What will be the output of the following code? 1 point

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 6/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

4 2 ('map', 'torch')
4 2 ('torch', 'map')
2 2 ('map', 'torch')
4 4 ('map', 'torch', 'torch', 'map')

20) Dilrez does the following: 1 point

Which of the following is true based on this setup?

All three containers contain the same items and in the same order
Only the pouch automatically removes duplicates
backpack[1] and supply_list[1] give the same result
Sets can be indexed just like lists and tuples

Himmat was given a task: Find the factorial of a number.

He read that:

The factorial of a number n is written as n!


It means:

n! = n × (n-1) × (n-2) × ... × 1

So, for example:

4! = 4 × 3 × 2 × 1 = 24

But instead of doing it manually, Himmat decided to write a recursive


function — a function that calls itself.

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 7/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

21) Why does the code return 1 when n == 0? 1 point

Because 0 × anything is 0
To stop the recursion at some point
By mistake; it should return 0
It helps to calculate square of the number

22) What will be the return value of factorial(1)? 1 point

0
1
It will call itself infinitely
2

23) Identify the error in the following code: 1 point

It will return 6
No base case defined, will result in infinite recursion
n is not decremented
Syntax error

24) What does the line n! = n × (n-1) × (n-2) × ... × 1 represent in the comprehension? 1 point

An infinite series
A summation of natural numbers
A recursive definition of factorial
A non-recursive mathematical shortcut

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 8/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

25) 1 point

How many recursive calls are made when he executes factorial(4)?

3
4
5
Infinite

26) Himmat accidentally writes: 1 point

What will happen if he calls factorial(0)?

Returns 1
Returns 0
Throws a recursion error
Returns -1

27) What would be a correct way to make Himmat’s function handle negative numbers 1 point
safely?

Add return -1 for all inputs


Add a base case if n < 0: return "Invalid input"
Remove recursion altogether
Multiply by -1 inside the function

28) If Himmat’s function is modified as: 1 point

What happens when factorial(3) is called?

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 9/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

Returns 6
Returns 0
Infinite recursion
Error due to syntax

29) Himmat runs this code to reverse a string: 1 point

What will be the output?

"a"
""
Error (index out of range)
"aa"

30) Himmat wants to reverse the word "race" using recursion. He writes a function that checks 1 point
whether the string is non-empty as the base case.

What will be the output of reverse_string("race")?

"race"
"ecar"
"rcae"
Error due to slicing empty string

You may submit any number of times before the due date. The final submission will be considered for
grading.
Submit Answers

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 10/11
9/10/25, 11:15 PM The Joy of Computing using Python - - Unit 10 - Week 7

https://onlinecourses.nptel.ac.in/noc25_cs103/unit?unit=143&assessment=603 11/11

You might also like