11  Nurturing Session I

%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


%%file box.py
# your code here

import sys
sys.argv
Writing box.py
%verify_problem text-in-a-box
Found 3 checks
✗ python box.py python
Expected:
+--------+
| python |
+--------+
Found:

✗ python box.py Joy
Expected:
+-----+
| Joy |
+-----+
Found:

✗ python box.py "Joy of Programming"
Expected:
+--------------------+
| Joy of Programming |
+--------------------+
Found:

💥 Oops! Your solution to problem text-in-a-box is incorrect or incomplete.
words = ["one", "two", "three", "four"]
words[1:]
['two', 'three', 'four']
%%file box.py
# your code here

import sys
print(sys.argv)
Overwriting box.py
!python box.py hello these are some arguments
['box.py', 'hello', 'these', 'are', 'some', 'arguments']
%%file box.py
# your code here

import sys
print(sys.argv[1:])
Overwriting box.py
!python box.py hello some more args
['hello', 'some', 'more', 'args']
def print_a_box(text):
    pass

print_a_box()
words
['one', 'two', 'three', 'four']
words
['one', 'two', 'three', 'four']
words[0]
'one'
text = "one two three four five"
text.split()
['one', 'two', 'three', 'four', 'five']
ones = [1, 1, 1, 1]
" ".join(words)
'one two three four'
%%file box.py
# your code here

import sys
arg_statement = " ".join(sys.argv[1:])
print(arg_statement)
Overwriting box.py
!python box.py hello this is another execution
hello this is another execution

11.0.1 methods from string

text 
'one two three four five'
text.split(" ")
['one', 'two', 'three', 'four', 'five']
text.split() # if no argument is given it will split on white space
['one', 'two', 'three', 'four', 'five']
"-".join(words) # it assumes that the list that you are passing as argument to join method has all text items in it
'one-two-three-four'
text
'one two three four five'
text.replace("o", "O")
'One twO three fOur five'
'one-two-three-four'.split()
['one-two-three-four']
'one-two-three-four'.split("-")
['one', 'two', 'three', 'four']

11.0.2 functions

def functioname(x, y):
    return x*y/100
def foo(x, y, z):
    s = (x + y + z)/3
    print(s) # make sure that your function returns
foo(1, 2, 3) == 2.0
2.0
False
def foo(x, y, z):
    s = (x + y + z)/3
    return s # make sure that your function returns
foo(1, 2, 3) == 2.0
True

11.0.3 loops

words
['one', 'two', 'three', 'four']
text
'one two three four five'
ones
[1, 1, 1, 1]
for i in ones:
    print(i)
1
1
1
1
for w in words:
    print(w)
one
two
three
four
def findwords_of_len(words, n):
    words_ = []
    for w in words:
        if len(w) == n:
            words_.append(w)
    return words_
findwords_of_len(words, 3)
['one', 'two']
findwords_of_len(words, 4)
['four']
"one" in words
True
"one" not in words
False

11.0.4 conversions and methods from string

111.count(1)
  Cell In[48], line 1
    111.count(1)
       ^
SyntaxError: invalid decimal literal
str(111).count(1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[49], line 1
----> 1 str(111).count(1)

TypeError: must be str, not int
str(111).count("1")
3
text
'one two three four five'
name = "python"
name[1:3] # it will give me substring which start at index 1 and stops at 2
'yt'
name[:] # copy
'python'
name[:2] # start at 0 and end at 1 ... take first two
'py'
name[2:] # drop frist two
'thon'
big = "asdkjahsdjkas sfjkadhfjkjhgsdfa"
big[4:15:2]
'jhdkss'
big[::-1]
'afdsghjkjfhdakjfs sakjdshajkdsa'
name[::-1]
'nohtyp'
nums = [1, 2, 3, 4, 5]
sqr = []
for n in nums:
    sqr.append(n*n)
"="*5
'====='

11.0.5 modules

import string
string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits
'0123456789'