Posts

Showing posts from January, 2019

Python - Week 4 - Multiplication Variants and Arrays

As I traveled to Canada, I read about arrays in Python. I also learned about how * operator is applied to repeat / concatenate. Here is the problem statement: Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. Suppose the following inputs are given to the program: 3,5 Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] Here is the code, thanks to gist on github . Some notes: In Python * means multiply for numbers, but repeat for strings—it’s like concatenating a string to itself repeatedly. The range built-in function generates a list of successive integers, starting from 0 be default and ending at upper bound - 1.

Python - Week 3 - Lists, Tuples, Slicing, Math Functions

This week I tried another problem from github: Write a program that calculates and prints the value according to the given formula: Q = Square root of [(2 * C * D)/H] Following are the fixed values of C and H: C is 50. H is 30. D is the variable whose values should be input to your program in a comma-separated sequence. Example Let us assume the following comma separated input sequence is given to the program: 100,150,180 The output of the program should be: 18,22,24 Some interesting findings from this program: The import in Python is done using 'from ... import ..' statement. This is used in our program to use round and sqrt functions. Tuple is an immutable sequence type. Functionally, they’re used to represent fixed collections of items. print function takes an argument which by default is '\n'. In our case, we are using a different end character ',' Slicing is a way to extract an entire section (slice) in a single step. printSeries[0:-1] - is ...

Python - Week 2 - Comprehensions

Python provides multiple idioms to get things done. Comprehensions is one such construct. Lets look at how it is used in simple ways. With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included).  And then the program should print the dictionary.  Suppose the following input is supplied to the program: 8  Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} What did we learn here: print was a statement in 2.x. In 3.x it is a function. Dictionaries store objects by key. Because dictionaries are unordered collections, operations that depend on a fixed positional order (e.g., concatenation, slicing) don’t make sense. Instead, dictionaries are the only built-in, core type representatives of the mapping category — objects that map keys to values.

Python - Week 1 - Using for loop and range function

This is the first in the series of Python programs I have written. Did some basic reading on Python to familiarize myself with Python syntax. Based on what I know so far I am using Python functions, for loop, range function, if statements, remainder operator and print method. The problem I am trying to solve is borrowed from github: Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5 between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. There are some key takeaways from this program: When typing Python programs, be sure to start all your unnested statements in column 1 (that is, all the way to the left). If you don’t, Python may print a “SyntaxError” message, because blank space to the left of your code is taken to be indentation that groups nested statements. There are no type declarations in Python, the syntax of the expressions you run determine...