Problem 1: Flatten the list

Module 1 - Nurturing session

How to take inputs from user?

x = input("Enter value for x:") # any value that you enter using input function is text
Enter value for x: 10
x
'10'
int(x) # ths has to be done manually
10

Another way, command line arguments

%%file say_hello.py
import sys

# argv variable inside sys module will have all the commandline arguments that you pass from commandline
sys.argv

print(sys.argv)
Writing say_hello.py
%%file say_hello.py
import sys

# argv variable inside sys module will have all the commandline arguments that you pass from commandline
sys.argv

print("Hello", sys.argv[1])
Overwriting say_hello.py
!python say_hello.py arcesium
Hello arcesium
%load_problem text-in-a-box
Problem: Text in a Box

Write a program box.py that takes word as a command-line argument and prints the word in a box as shown below.

$ python box.py python
+--------+
| python |
+--------+

Please note that there should be exactly one space on either side of the text in the box.

You can verify your solution using:

%verify_problem text-in-a-box

%%file box.py
# your code here
import sys

def print_a_box1(word):
    dashcount = len(word)+2
    line1 = "+" +  "-"*dashcount + "+"
    line2 = "| " + word + " |"
    print(line1)
    print("|",word,"|")
    print(line1)

word = sys.argv[1] # get the word from command line 

print_a_box1(word) # call the function with that word as argument
Overwriting box.py
!python box.py hello
+-------+
| hello |
+-------+
def print_a_box(word):
    dashcount = len(word)+2
    line1 = "+" +  "-"*dashcount + "+"
    line2 = "| " + word + " |"
    print(line1)
    print(line2)
    print(line1)
def print_a_box1(word):
    dashcount = len(word)+2
    line1 = "+" +  "-"*dashcount + "+"
    line2 = "| " + word + " |"
    print(line1)
    print("|",word,"|")
    print(line1)
print_a_box("Python")
+--------+
| Python |
+--------+
print_a_box1("Python")
+--------+
| Python |
+--------+
%verify_problem text-in-a-box
✓ python box.py python
✓ python box.py Joy
✓ python box.py "Joy of Programming"
🎉 Congratulations! You have successfully solved problem text-in-a-box!!
!python box.py Arcesium
+----------+
| Arcesium |
+----------+
%load_problem mean
Problem: Mean

Write a function mean to compute the arthematic mean of a list of numbers.

The arthemetic mean of N numbers is computed by adding all the N numbers and dividing the total by N.

The function takes the list of numbers as argument and returns the mean.

>>> mean([1, 2, 3, 4, 5])
3.0

You can verify your solution using:

%verify_problem mean

# your code here

def mean(nums):
    m = sum(nums)/len(nums)
    print(m) # it should return
mean([1, 2, 3, 4, 5])
3.0
%verify_problem mean
3.0
✗ mean([1, 2, 3, 4, 5])
  expected: 3.0
  found: None
5.0
✗ mean([5])
  expected: 5.0
  found: None
💥 Oops! Your solution to problem mean is incorrect or incomplete.
x = mean([1, 2, 3, 4, 5, 6])
3.5
print(x)
None

Any function that does not return a value, actually returns None

Flatten the list

items = [[1, 1, 2], [2, 3, 4], [2,3]]
empty = []
empty.extend(items[0])
empty
[1, 1, 2]
for item in items:
    empty.extend(item)

for loop

nums = range(10)
words = ["one", "two", "three", "four"]
for w in words:
    print(w.upper())
ONE
TWO
THREE
FOUR

Unique

items = [1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
if "one" in words:
    print("one is in words!")
one is in words!
unique = []
for item in items:
    if item not in unique:
        unique.append(item)


        
unique
[1, 2, 3, 4]

minimum3

x, y = 5, 6
def min2(x, y):
    if x < y:
        return x
    else:
        return y
min2(56, 3)
3
min2(-1, -43)
-43
43%2
1
42%2
0
def even(n):
    return n%2==0
even(42)
True

How to take multipule arguments

%%file args.py
import sys

print(sys.argv[1])
Writing args.py
!python args.py 
Traceback (most recent call last):
  File "/home/jupyter-vikrant/arcesium-python-2024/args.py", line 3, in <module>
    print(sys.argv[1])
IndexError: list index out of range
!python args.py arg1
arg1
words
['one', 'two', 'three', 'four']
words[1]
'two'
words[1:]
['two', 'three', 'four']
%%file args.py
import sys

print(sys.argv[1:])
Overwriting args.py
!python args.py I want to give this sentence
['I', 'want', 'to', 'give', 'this', 'sentence']
%%file args.py
import sys

print(" ".join(sys.argv[1:]))
Overwriting args.py
!python args.py I want to give this sentence
I want to give this sentence
%%file single_arg.py
import sys

print(sys.argv[1])
Writing single_arg.py
!python single_arg.py A sentence like this will not work
A
!python single_arg.py "A sentence like this will not work"
A sentence like this will not work