0% found this document useful (0 votes)
16 views3 pages

Programming with Generative AI Course

The document outlines the structure and assignments for the NPTEL course 'Programming with Generative AI', detailing weekly topics, assignments, and examples of Python and C programming tasks. It includes specific instructions for assignments, expected outputs, and common errors to address in code translation between Python and C. Additionally, it provides hints and accepted answers for various programming challenges presented throughout the course.

Uploaded by

Prashanth H A
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)
16 views3 pages

Programming with Generative AI Course

The document outlines the structure and assignments for the NPTEL course 'Programming with Generative AI', detailing weekly topics, assignments, and examples of Python and C programming tasks. It includes specific instructions for assignments, expected outputs, and common errors to address in code translation between Python and C. Additionally, it provides hints and accepted answers for various programming challenges presented throughout the course.

Uploaded by

Prashanth H A
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

X

([Link] ([Link]

prashanth049@[Link] 

NPTEL ([Link] » Programming with Generative AI (course)

Course outline Week 7 Assignment 7


The due date for submitting this assignment has passed.
Week 1 () Due on 2025-10-08, 23:59 IST.

How does an NPTEL


online course work? ()
Assignment submitted on 2025-10-06, 22:14 IST
1) First read this ([Link] blog post to understand the difference between the isdigit, isnumeric, 1 point
Week 1 Part 1 and isdecimal methods.
Introduction ()
Now consider the following AI-generated body for the Python function below:
Week 1 Part 2 Getting
started with Python () def unique_digits(s: str) -> int:
"""Count the number of
Week 2 Part 1 Variables unique digits in s.
and Assignments () >>> unique_digits('15 Aug 1947')
5
Week 2 Part 2 Functions """
and Simple Programs () return len(set(c for c in s if [Link]())) # AI suggestion

Week 3 Part 1 User-


Our client wants the function to satisfy the following doctests:
defined functions ()
>>> unique_digits('The equation is x ** 2 = y')
Week 3 Part 2 Refuting 1
and debugging complex
>>> unique_digits('The equation is x² = y')
functions ()
1
>>> unique_digits('We had 5 subjects in Class 5')
Week 4 Part 1 Helper
1
functions and recursion
>>> unique_digits('We had 5 subjects in Class V')
()
1
>>> unique_digits('We had 5 subjects in Class Ⅴ')
Week 4 Part 2 Asking
2
Clarifying Questions ()

What change (if any) needs to be made to the AI suggestion to satisfy all doctests?
Week 5 Part 1 Lists,
Tuples, and Exceptions
No changes
()
Replace [Link]() with [Link]()

Week 5 Part 2 Iteration () Replace [Link]() with [Link]()


Replace [Link]() with '0' <= c <= '9'
Week 6 Iteration and
Replace set with list
Testing ()
Replace set with tuple
Week 7 Learning a new No, the answer is incorrect.
language (C) () Score: 0
Accepted Answers:
Clarifying the task: Replace [Link]() with [Link]()
count_between (unit?
unit=63&lesson=56)
2) Your friend has written the following program in C: 1 point
Understanding the
interpretation of "between" 1:
(unit?unit=63&lesson=229) 2: void main() {
3: int x = 0;
Are ChatGPT suggestions
"good"? (unit?
4: printf("x = %d", x);
unit=63&lesson=230) 5:
6: }
Generating the code after
clarifying (using ChatGPT)
Select all changes that your friend should make to improve this program.
(unit?unit=63&lesson=231)

(Line 1) Delete
Additional ChatGPT inputs (Line 1) Add: #include <stdio.h>
(unit?unit=63&lesson=232)
(Line 1) Add: import stdio.h
Revised code after (Line 2) Rewrite as: def main() -> int {
additional clarifications
(Line 2) Rewrite as: int main() {
(unit?unit=63&lesson=233)
(Line 2) Rewrite as: def main(int) {
Learning a new
programming language (Line 3) Rewrite as: x = 0;
(unit?unit=63&lesson=234) (Line 3) Rewrite as: x: int = 0;

Python vs C: Similarities (Line 3) Rewrite as: int x;


and differences (unit? (Line 4) Rewrite as: printf("x = {x}");
unit=63&lesson=235)
(Line 4) Rewrite as: printf("x = %d\n");
Python: Example 1 (unit?
(Line 4) Rewrite as: printf("x = %d\n", x);
unit=63&lesson=236)
(Line 5) Add: return;
Translating Example 1 to C
(unit?unit=63&lesson=237)
(Line 5) Add: return 0;
(Line 5): Delete
Key syntactic differences:
Python vs C (unit? No, the answer is incorrect.
unit=63&lesson=238) Score: 0
Accepted Answers:
Visualizing Python and C (Line 1) Add: #include <stdio.h>
code (unit?
(Line 2) Rewrite as: int main() {
unit=63&lesson=239)
(Line 4) Rewrite as: printf("x = %d\n", x);
C programming style (unit? (Line 5) Add: return 0;
unit=63&lesson=240)
3) Consider the following Python program:
Python: Example 2 (unit?
unit=63&lesson=241)
def factorial(n: int) -> int:
Example 2, Attempt 2 (unit? if n <= 1:
unit=63&lesson=242) return 1
return n * factorial(n - 1)
Semantics of integer
division (unit?
if __name__ == '__main__':
unit=63&lesson=243)
n = 4
Example 2, Attempt 3 (unit? print(factorial(n))
unit=63&lesson=244)

Summary (unit? Your friend has tried to translate this program into C (PythonTutor
unit=63&lesson=245) ([Link]
%201%29%3B%0A%7D%0A%0Aint%20main%28%29%20%7B%0A%20%20int%20n%20%3D%204%3B%0A%20%20printf%28factorial%28n%29%29%
Quiz: Week 7 Assignment
[Link]&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false)).
7 (assessment?
name=246)
First fix the error in this program. (Hint: If you cannot spot the error, click on Visualize Execution and see the line that causes the error. If you cannot
Weekly Feedback Form fix this error your self, seek help from PythonTutor's AI Tutor.)
(unit?unit=63&lesson=133)

After fixing the C program, identify the smallest positive integer n for which the Python program and the C program produce different answers.
Week 8 Part 1 Strings
and functions in C () 13

Week 8 Part 2 Hint


Demystifying Python
Lists ()
Yes, the answer is correct.
Score: 1
Live Session () Accepted Answers:
(Type: String) 13

1 point

4) Consider the following Python program:

def num_digits(n: int) -> int:


if n < 0:
n = -n
result = 1
while n >= 10:
result += 1
n //= 10
return result

if __name__ == '__main__':
n = -100
print(num_digits(n))

Your friend has tried to translate this program into C (PythonTutor


([Link]
n%3B%0A%20%20%7D%0A%20%20int%20result%20%3D%201%3B%0A%20%20while%20%28n%20%3E%3D%2010%29%20%7B%0A%20%20%20%
100%3B%0A%20%20printf%28%22%25d%5Cn%22,%20num_digits%28n%29%29%3B%0A%20%20return%200%3B%0A%7D&cumulative=false&heapP
[Link]&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false)). In PythonTutor, integers in C are represented using 32 bits. You
can read about the range of representable C integers here ([Link]
First fix the error in your friend's program. (Hint: If you cannot spot the error, click on Visualize Execution and see the line that causes the error. If
you cannot fix this error your self, seek help from PythonTutor's AI Tutor.)

After fixing the C program, identify the only integer n that is representable in C for which the Python program and the C program
produce different answers.

−2,147,483,648

Hint

No, the answer is incorrect.


Score: 0
Accepted Answers:
(Type: String) -2147483648
1 point

Common questions

Powered by AI

The use of 'isnumeric()' instead of 'isdigit()' in the function 'unique_digits' allows the function to recognize numeric characters beyond just simple ASCII digits ('0'-'9'). Specifically, 'isnumeric()' can identify Unicode digits like superscripts and subscripts which 'isdigit()' might not recognize, thus satisfying all doctest cases including those with characters like 'Ⅴ' and '²'. This change ensures correct counting of numeric representations in strings with diverse character sets .

Understanding zero-based indexing in C is crucial for proper data manipulation and loop control, preventing off-by-one errors. Zero-based indexing aids direct access calculation using base address arithmetic conducive for pointer manipulation. Contrastingly, one-based indexing, common in some other programming paradigms, aligns more naturally with human counting, causing discrepancies when algorithms assume different bases. Recognizing these differences informs adjustments in array traversal, boundary conditions, and recursive base cases, ensuring cross-language algorithm consistency and correctness .

Translating a Python function utilizing recursion, such as 'factorial', into C can be challenging due to Python's dynamic typing and automatic memory management which contrasts with C's static typing and manual resource handling. C requires explicit function prototypes and care must be taken with stack size due to recursion. Additionally, Python automatically handles large integers beyond fixed sizes, whereas C has defined limits causing integer overflow in recursion functions if not managed appropriately. Such behavior differences can result in different outputs when functions deal with large numbers, as noted when calculating factorial for large 'n' .

Incorporating header files in C enhances modularity by allowing code separation, enabling function declarations, and type definitions to be reused across multiple source files. This separation encourages clearer design structure, reduces redundancy, and centralizes code changes, improving maintainability. It also facilitates collaborative development by providing a stable interface for dependencies, allowing independent module compilation. Preprocessor directives include these headers, ensuring consistent function signatures and macros, preserving program integrity across modules .

C’s explicit declaration of data types enhances error checking, memory management precision, and optimized performance as the compiler knows variable types during compilation. This commonly leads to faster execution and decreased likelihood of runtime type-related errors. However, it introduces rigidity, as one must declare and manage types explicitly, which can complicate code readability and increase verbosity. In contrast, Python's dynamic typing offers flexibility, reduced boilerplate code, and ease of rapid prototyping, albeit at potential run-time type errors and overhead in managing variable storage dynamically .

Python's 'isdecimal()' checks if all characters are decimal, meaning numbers without fractions or separators, typically '0'-'9'. 'isdigit()' recognizes decimal characters and digits from different scripts. 'isnumeric()' supports even a wider range including numerals expressed in Unicode, such as fractions, Roman numerals, or numeric characters in other languages. In text processing, 'isnumeric()' is most comprehensive for recognizing numeric data in diverse character sets, whereas 'isdecimal()' is suitable for basic digit validation, impacting applications requiring internationalization or dealing with mathematical symbols .

Technical challenges in translating formatted print statements from Python to C include managing format specifiers specific to C's 'printf' function. Python's string formatting is more forgiving and versatile, allowing expressions and complex objects, while C requires strict adherence to format specifiers (e.g., '%d', '%f') and manual type specification. Mitigation involves ensuring correct format specifier usage matching variable types and including necessary header files (e.g., <stdio.h>). Moreover, care must be taken not to assume automatic handling of newline characters, requiring explicit '\n' .

Comparisons reveal that Python, with its high-level constructs, dynamic typing, and rich libraries, is well-suited for rapid application development, scripting, data analysis, and applications where productivity and ease of use are prioritized. It excels in areas like web development and scientific computing. C, with its low-level memory management, static typing, and efficiency, suits system programming, embedded systems, and performance-critical applications where control and resource usage minimization are crucial. Such differences impact language choice based on application needs, with Python favored for speed of development and C for performance .

To improve the C program's functional correctness and adhere to programming conventions, several changes are necessary: Add '#include <stdio.h>' to include the standard input-output library; Modify 'void main()' to 'int main()' as the standard entry point for C programs; Adjust the 'printf' statement to 'printf("x = %d\n", x);' to correctly format and print the integer variable; Finally, add 'return 0;' at the end of the main function to indicate successful program termination .

Using 'set()' in Python’s 'unique_digits' function efficiently counts unique characters by automatically handling duplicates, leveraging sets' inherent property of uniqueness which reduces the need for explicit duplicate checks. This improves performance through average O(1) time complexity for additions, compared to O(n) in list-based approaches, optimizing scenarios with large input strings. The concise syntax simplifies readability and enhances maintainability, aligning with Python's emphasis on code simplicity and elegance .

You might also like