# NPTEL Programming, Data Structures And Algorithms Using Python Week2 Assignment

NPTEL Course is an introduction to programming and problem solving in Python.  It does not assume any prior knowledge of programming.  Using some motivating examples, the course quickly builds up basic concepts such as conditionals, loops, functions, lists, strings and tuples.  It goes on to cover searching and sorting algorithms, dynamic programming and backtracking, as well as topics such as exception handling and using files.  As far as data structures are concerned, the course covers Python dictionaries as well as classes and objects for defining user defined datatypes such as linked lists and binary search trees.

### Programming, Data Structures And Algorithms Using Python Week2 Assignment Jan 2024

INTENDED AUDIENCE :  Students in any branch of mathematics/science/engineering, 1st year
PREREQUISITES : School level mathematics
INDUSTRY SUPPORT :  This course should be of value to any company requiring programming skills.

## Course Layout

Week 1:
Informal introduction to programming, algorithms and data structures via GCD
GCD in Python: variables, operations, control flow – assignments, conditionals, loops, functions

Week 2:
Python: types, expressions, strings, lists, tuples
Python memory model: names, mutable and immutable values
List operations: slices etc
Binary search
Inductive function definitions: numerical and structural induction
Elementary inductive sorting: selection and insertion sort
In-place sorting

Week 3:
Basic algorithmic analysis: input size, asymptotic complexity, O() notation
Arrays vs lists
Merge sort
Quicksort
Stable sorting

Week 4:
Dictionaries
More on Python functions: optional arguments, default values
Passing functions as arguments
Higher order functions on lists: map, lter, list comprehension

Week 5:
Exception handling
Basic input/output
Handling files
String processing

Week 6:
Backtracking: N Queens, recording all solutions
Scope in Python: local, global, non-local names
Nested functions
Data structures: stack, queue
Heaps

Week 7:
Abstract data-types
Classes and objects in Python
Binary search trees: find, insert, delete
Height-balanced binary search trees

Week 8:
Efficient evaluation of recursive definitions: memorization
Dynamic programming: examples
Other programming languages: C and manual memory management

# Programming Assignment 1

Write three Python functions as specified below. Paste the text for all three functions together into the submission window. Your function will be called automatically with various inputs and should return values as specified. Do not write commands to read any input or print any output.

• You may define additional auxiliary functions as needed.
• In all cases you may assume that the value passed to the function is of the expected type, so your function does not have to check for malformed inputs.
• For each function, there are normally some public test cases and some (hidden) private test cases.
• “Compile and run” will evaluate your submission against the public test cases.
• “Submit” will evaluate your submission against the hidden private test cases. There are 12 private test cases, with equal weightage. You will get feedback about which private test cases pass or fail, though you cannot see the actual test cases.
• Ignore warnings about “Presentation errors”.

1. A positive integer m can be expressed as the sum of three squares if it is of the form p + q + r where p, q, r ≥ 0, and p, q, r are all perfect squares. For instance, 2 can be written as 0+1+1 but 7 cannot be expressed as the sum of three squares. The first numbers that cannot be expressed as the sum of three squares are 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, … (see Legendre’s three-square theorem).

Write a Python function threesquares(m) that takes an integer m as input and returns True if m can be expressed as the sum of three squares and False otherwise. (If m is not positive, your function should return False.)

Here are some examples of how your function should work.

```>>> threesquares(6)
True

>>> threesquares(188)
False

>>> threesquares(1000)
True
```
2. Write a function repfree(s) that takes as input a string s and checks whether any character appears more than once. The function should return True if there are no repetitions and False otherwise.

Here are some examples to show how your function should work.

```
>>> repfree("zb%78")
True

>>> repfree("(7)(a")
False

>>> repfree("a)*(?")
True

False
```
3. A list of numbers is said to be a hill if it consists of an ascending sequence followed by a descending sequence, where each of the sequences is of length at least two. Similarly, a list of numbers is said to be a valley if it consists of an descending sequence followed by an ascending sequence. You can assume that consecutive numbers in the input sequence are always different from each other.

Write a Python function hillvalley(l) that takes a list l of integers and returns True if it is a hill or a valley, and False otherwise.

Here are some examples to show how your function should work.

```>>> hillvalley([1,2,3,5,4])
True

>>> hillvalley([1,2,3,4,5])
False

>>> hillvalley([5,4,1,2,3])
True

>>> hillvalley([5,4,3,2,1])
False```
```				```
# Function to check if a number can be expressed as the sum of three squares
def threesquares(m):
if m < 0:
return False
for i in range(0, int(m**0.5)+1):
for j in range(i, int(m**0.5)+1):
for k in range(j, int(m**0.5)+1):
if i*i + j*j + k*k == m:
return True
return False

# Function to check if a string has no repeated characters
def repfree(s):
return len(s) == len(set(s))

# Function to check if a list is a hill or a valley
def hillvalley(l):
if len(l) < 3:
return False
# Check for hill
if l[0] < l[1] and l[-1] < l[-2]:
peak_reached = False
for i in range(len(l)-1):
if not peak_reached:
if l[i] >= l[i+1]:
peak_reached = True
if peak_reached:
if l[i] <= l[i+1]:
return False
return True
# Check for valley
elif l[0] > l[1] and l[-1] > l[-2]:
trough_reached = False
for i in range(len(l)-1):
if not trough_reached:
if l[i] <= l[i+1]:
trough_reached = True
if trough_reached:
if l[i] >= l[i+1]:
return False
return True
return False

```
```