Python Programming Series # 2 - Installation guide, Warmup Series -1 & 2

in #python9 years ago (edited)

python programming series_2.png

This is the continuation of the previous tutorial of the Python programming series -
So, please refer part 1 before any further.

Installation

Before starting the tutorial, I would like to go through the simplest Python Installation goto guide.
Just download Anaconda platform from this website.

Beginner's guide for Anaconda - https://classroom.udacity.com/courses/ud1111/

This will be also useful for 'Data Science' subject as well. There are many libraries, not only for Python, but also other languages as well - Javascript, etc.

Here, the notebook is 'Jupyter' which can be accessed from the command shell as follows:

jupyter notebook   # syntax for opening jupyter notebook, and launched in the browser.

This is a browser-based editor and a kernel associated with it for compilation.

Also, here another advantage is we can make separate environments for Python2 and Python3
using the following commands in the command shell for different OS - Mac, Linux, Windows:

conda create -n py2 python=2    # installs the latest version of python 2 by default
conda create -n py3 python=3    # installs the latest version of python 3 by default

And activating & deactivating the environments using the following syntaxes: -

activate py2  # activating the python2 environment named 'py2'
deactivate py2  # deactivating the activated environment 'py2'
activate py3  # activating the python3 environment named 'py3'
deactivate py3  # deactivating the activated environment 'py3'

So, in this way, the python of whichever version can be executed on the same system.

Coding

Warmup-1 > front_back_reverse

Question:
Given a string, find the reversed string.

Check for these cases:
front_back_rev('code') → 'eodc'
front_back_rev('a') → 'a'
front_back('ab') → 'ba'

Solution:

def front_back_rev(str): 
    rstr = ""  # initialized the unused variable.
    for i in range(len(str)):
        rstr += str[(len(str)-1)-i]
    print(rstr)

front_back_rev('code') #→ 'edoc'
front_back_rev('a') #→ 'a'
front_back_rev('ab') #→ 'ba'

#----------------------------------------#

#----------------------------------------#

Warmup-1 > front_back

Question:
Given a string, return a new string where the first and last chars have been exchanged.

Check for these cases:
front_back('code') → 'eodc'
front_back('a') → 'a'
front_back('ab') → 'ba'

Solution:

def front_back(str):
    if(len(str)<=1):  # if the string is of single character, print the same string.
        print(str)
    else:
        front = str[0]  # 1st character
        mid = str[1:len(str)-1]
        last = str[len(str)-1] # last character
        new_str = last + mid + front # string added in reverse order, with middle part same as previous.
        print(new_str)

front_back('code') #→ 'eodc'
front_back('a') #→ 'a'
front_back('ab') #→ 'ba'

#----------------------------------------#

#----------------------------------------#

Warmup-1 > front3

Question:
Given a string, we'll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.

Check for these cases:
front3('Java') → 'JavJavJav'
front3('Chocolate') → 'ChoChoCho'
front3('abc') → 'abcabcabc'

Solution:

def front3(str):
    if(len(str)>=3): # if the same string has 3 or more characters string, print the required string
        print(3 * str[0:3])  # str[0:3] means from str[0] to str[2]
    else:          # if the string has less than 3 character string, print the same string.
        print(3*str)

front3('Java') #→ 'JavJavJav'
front3('Chocolate') #→ 'ChoChoCho'
front3('abc') #→ 'abcabcabc'

#----------------------------------------#

#----------------------------------------#

Warmup-2 > string_times

Question:
Given a string and a non-negative int n, return a larger string that is n copies of the original string.

Check for these cases:
string_times('Hi', 2) → 'HiHi'
string_times('Hi', 3) → 'HiHiHi'
string_times('Hi', 1) → 'Hi'

Solution:

def string_times(str, n):
    if(n>0):
        print(n * str)
    else:
        return -1

string_times('Hi', 2) #→ 'HiHi'
string_times('Hi', 3) #→ 'HiHiHi'
string_times('Hi', 1) #→ 'Hi'

#----------------------------------------#

#----------------------------------------#

Warmup-2 > front_times

Question:
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

Check for these cases:
front_times('Chocolate', 2) → 'ChoCho'
front_times('Chocolate', 3) → 'ChoChoCho'
front_times('Abc', 3) → 'AbcAbcAbc'

Solution:

def front_times(str, n):
    if(n>0 and str != ""):
        front = str[0:3]
        print(n * front)
    else:
        return -1

front_times('Chocolate', 2) #→ 'ChoCho'
front_times('Chocolate', 3) #→ 'ChoChoCho'
front_times('Abc', 3) #→ 'AbcAbcAbc'

#----------------------------------------#

#----------------------------------------#

Warmup-2 > string_bits

Question:
Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".

Check for these cases:
string_bits('Hello') → 'Hlo'
string_bits('Hi') → 'H'
string_bits('Heeololeo') → 'Hello'

Solution:

def string_bits(str):
    nstr = ""
    for i in range(0,len(str),2):   # e.g. len = 5, so, i = 0,2,4
        nstr += str[i]
    print(nstr)

string_bits('Hello') #→ 'Hlo'
string_bits('Hi') #→ 'H'
string_bits('Heeololeo') #→ 'Hello'

#----------------------------------------#

#----------------------------------------#

Warmup-2 > string_splosion

Question:
Given a non-empty string like "Code" return a string like "CCoCodCode".

Check for these cases:
string_splosion('Code') → 'CCoCodCode'
string_splosion('abc') → 'aababc'
string_splosion('ab') → 'aab'

Solution:

def string_splosion(str):
    nstr = ""
    for i in range(len(str)):
        nstr += str[:i+1]     # gives 'CCoCodCode'
        #nstr += str[0:len(str)-i]  # gives 'CodeCodCoC'
    print(nstr)

string_splosion('Code') #→ 'CCoCodCode'
string_splosion('abc') #→ 'aababc'
string_splosion('ab') #→ 'aab'

#----------------------------------------#

#----------------------------------------#

Warmup-2 > array_count9

Question:
Given an array of ints, return the number of 9's in the array.

Check for these cases:
array_count9([1, 2, 9]) → 1
array_count9([1, 9, 9]) → 2
array_count9([1, 9, 9, 3, 9]) → 3

Solution:

def array_count9(nums):
    count = 0
    for num in nums:
        if(num == 9):
            count = count + 1
    print(count)
    
array_count9([1, 2, 9]) #→ 1
array_count9([1, 9, 9]) #→ 2
array_count9([1, 9, 9, 3, 9]) #→ 3

#----------------------------------------#

#----------------------------------------#

Stay tuned for next tutorial.... :)

Follow Github

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.079
BTC 63789.74
ETH 1693.74
USDT 1.00
SBD 0.41