Python Week 12 Programming Assignment Solutions

NPTEL Joy of Computing Using Python Week 12 Programming Assignment Solutions 2026 (Complete Code & Explanation)

Introduction

If you are working on the Python Week 12 Programming Assignment Solutions, you might find some questions a bit tricky—especially if you are new to Python logic building. Don’t worry! In this blog, we will cover all three programming assignments in a simple, beginner-friendly way with clean code and short explanations.

The goal is not just to help you submit your assignment, but also to understand the logic behind each solution so you can improve your problem-solving skills. We will break down each question step by step, explain the thought process, and highlight common mistakes so you can avoid them easily. By the end, you will feel more confident writing your own Python programs.

📘 Course Information

These assignments mainly test your understanding of:

  • Lists and dictionaries
  • Loops and conditions
  • Basic problem-solving logic

✅ Question 1: Dictionary Consistency Checker

🔹 Problem Summary

You are given a dictionary where each key has a list of integers.
A key is consistent if:

  • The list is strictly increasing
  • The difference between consecutive elements is the same
  • Lists with 0 or 1 element are always valid

👉 You need to count such keys.

💻 Code

records = eval(input())

count = 0

for key in records:
arr = records[key]

if len(arr) <= 1:
count += 1
else:
increasing = True
for i in range(1, len(arr)):
if arr[i] <= arr[i1]:
increasing = False
break

same_diff = True
diff = arr[1] arr[0]
for i in range(2, len(arr)):
if arr[i] arr[i1] != diff:
same_diff = False
break

if increasing and same_diff:
count += 1

print(count)

✏️ Explanation

  • Take dictionary input
  • Check each list
  • If size ≤ 1 → valid
  • Otherwise check increasing order and same difference
  • Count valid keys and print result

✅ Question 2: Score Conservation Checker

🔹 Problem Summary

You are given a directed graph and scores for each node.

Rules:

  • If a node has neighbors → divide score equally
  • If no neighbors → keep score
  • Add all incoming scores

👉 Check if total score remains the same after one step

💻 Code

n = int(input())

graph = {}
for _ in range(n):
data = list(map(int, input().split()))
node = data[0]
graph[node] = data[1:]

scores = {}
for _ in range(n):
node, val = map(int, input().split())
scores[node] = val

new_scores = {i: 0 for i in graph}

for node in graph:
if len(graph[node]) == 0:
new_scores[node] += scores[node]
else:
share = scores[node] / len(graph[node])
for nei in graph[node]:
new_scores[nei] += share

before = sum(scores.values())
after = sum(new_scores.values())

print(before == after)

✏️ Explanation

  • Store graph and scores
  • Distribute score equally among neighbors
  • Keep score if no outgoing edge
  • Compare total before and after
  • Print True or False

✅ Question 3: Longest Consecutive Sequence Finder

🔹 Problem Summary

You are given a list of integers.
Find the length of the longest consecutive sequence.

Rules:

  • Numbers must differ by 1
  • Order does not matter
  • Ignore duplicates

💻 Code

nums = eval(input())

nums_set = set(nums)

longest = 0

for num in nums_set:
if num 1 not in nums_set:
current = num
length = 1

while current + 1 in nums_set:
current += 1
length += 1

if length > longest:
longest = length

print(longest)

✏️ Explanation

  • Remove duplicates using set
  • Start sequence from smallest element
  • Count consecutive numbers
  • Track maximum length
  • Print result

🎯 Conclusion

The Week 12 assignments of “The Joy of Computing using Python” are designed to strengthen your core programming skills. These problems help you understand real logic building using Python concepts like loops, conditions, sets, and dictionaries.

By practicing these questions, you will:

  • Improve logical thinking
  • Write cleaner and optimized code
  • Handle edge cases effectively
  • Gain confidence for exams and coding interviews

👉 Final Tip: Don’t just copy the code—try to understand it and write it on your own. That’s the best way to learn programming! 🚀

For more Opportunities Click Here