• Read a new article every month, about topics like: Astonomy, Engineering, Science and Physics.
  • Checkout my original science fiction stories, that will take show you a whole new world.
  • Read about my experiments with different ideas, events and people.
  • Tinkering time! A detailed list of all the projects I am currently working on.
  • Boared? Checkout my the list of my favourate BOOKS and MOVIES!

Editorial for G.N.D.E.C. Student Chapter  Institute of Engineers, Code War-2.0

This is the editorial for the problems in Code War 2.0 conducted on 11 November, 2021. It discusses the approach to solve the problems, remarks and advise to solve the problems, that the writer could come up with at the time of writing of this blog. Suggestions or corrections in the following are welcome and one can convey the same by contacting Raghav Jit from CODEWAR 2.0 WhatsApp group.

  • 2 Power Check

2 power check problem required the contestant to write a code that could check if any number can be written in the form of 2^n, examples for the same are 4, 8, 16, 32 writable as 2^2, 2^3, 2^4, 2^5 respectively.

The code was supposed to take input in 'int' type as one cannot perform mathematical operation on string type. It started with a constraint (limitation) on the input number, which was the number to be between 0 and 2^100000 exclusive of limits. One should note that the mathematical operations we will conduct in the program (logarithm) can't be used on a negative number. To make sure that the number inputted is between the set limit we can use the if - elif - else of Python. First we will check if the number is above 0 and then if it is below 2^100000, for which we can either use nested if statements (one if statement after the other under the previous ones indentation) or we can use the keyword 'and' to get a 'True' if both cases satisfy and 'False' otherwise. 

Once the input is between the constraints we can proceed with checking if the number can be written as 2^n. If a number can be represented as 2^n then, taking log (base 2) on the number must return a whole number. Log2 function is not built in in Python so we should first import the 'math' library using 'import math' command at the beginning of the code and call 'math.log2(number)' to get the logarithm of the number

next we will check if the number is a whole number. There are two ways of doing this: either convert the number to a string and see if the string contains a '.' character or take the round() of the logarithm and check if it comes equal to the original number.
Then we can once again use if- else to print 'true' of  'false' in appropriate cases.

  • Bhuvan and Fleast Party

This Problem is similar to the previous one as we need to check if age of subcribers can be represented in form of 2^n

But first we need to take input for the program which is given in the format:

12 f, 14 b, 80b etc.

where 'f' and 'b' denote Flying Beast and Bhuvan Bam subscribers respectively. Using the if- else statements we can check if the subscriber is of Bhuvan of Fleast, for which we will use [] method to obtain the last letter of a string ('raghav'[-1], will print 'v'). Then we will use a nested if to check that the age of the subscriber is between 0 and 500, which can be done is exactly the same manner as in previous question however it will be more efficient to use 'and' keyword instead of another nested if, as it makes the code more readable. 

Next we need to check if the number is a whole number power of 2, and we will do it in the same way as mentioned above.

  • Ashish and Lock

This was a relatively harder problem which employees loops. In this question the participant was presented with a number pad / typing pad as below:
1 a, b, c, d
2 d, e, f, g
3 h, i, j, k
4 l, m, n, o
Where pressing 1 twice means 'b' and thrice means 'c', and the same works for all other keys 1, 2, 3, 4
now these keys one was supposed to generate all the possible combinations of letters.
We can use nested loops to solve this, but before that we need to define some variables:

First we will create 4 lists in the program that contains the letters under each of the individual keys, Eg: L1=['a','b','c','d'] for key 1. Also we have to make a dictionary with keys as key names and values to the keys as their respective lists, as 1: L1 so we can access the lists for those keys using this dictionary. Second we will declare a empty string (v=''), to which we will concatenate (join) each of the combinations to, so we can print them all at the end as a single string.

After taking a input which will be 4 digits in length (checking by if - else), we will start the nested loop of depth 4 (as we are taking input of length = 4)

At the end of the loops we will get a certain combination of our iterating variables (i, j, k, l) , one can first obtain the digits of input individually (using [], digit[0] will give first digit of the keypad), using this digit we will obtain the respective list from the dictionary. Now the list which we have obtained contains the elements as letters of the respective key, and we will access the letters on the basis of vales of i, j, k, l. 

Hence we will get one combination for our answer, and we will connect it to empty string 'v', we declared at the beginning, using the '+' operator. As we are supposed to print the combinations as comma separated single string we must add a ',' after each of the combinations before adding them to 'v'.

  • Decode The Pattern 5

This was a pattern based question, which required the participants to print a pattern. First we must make some observations of the sample patterns, it looks as if the pattern is of a rhombus but when larger numbers were inputted the pattern becomes more clear it is a matrix of stars '*' such that all the stars that fall on the diagonal are replaced with spaces. Once we know this we have a intuitive sense of how to approach the problem. Though there can be many ways to solve such pattern questions only one of them is discussed here, and it goes as follows.

First we take int type input and check it for constraints. Then we will produce a list of length equal to the input number exclusively containing "*". Moving forward we will use item assignment on the list. using a loop which is a for loop and will iterate input number of times, we will assign empty space to the first and last element of the list, then print the list as a string using ''.join(list) then undo the changes made on the list. Next when the loop will execute it will replace 2nd and last second place "*"  with a ' '. Eventually we will reach a point when we will replace either adjcent stars (if input is an even number) or just one star (if it is odd), after that the program will just run in reverse and replace second last and second place star with a white dash and first and last star with a dash at the end. 

  • Python if - Else

This problem is entirely if - else statement based. We need to make a flow chart / decision tree and using that we can decide what to do in each case.
First we will take a int type input then we will check it through various statements as:

1. If number is odd: we will check the remainder of the input when divided with 2 if it comes equal to   0 number is even otherwise odd, if it comes out to be odd we will print 'Weird'

2. Next we will use 'elif' instead of 'if ' this is because we want the code written under elif to execute if all the conditions before it are false (if we will use if here the code will execute even if the first if block had been executed, this will lead to redundancy in output.) Here we will use 'and' keyword to check if the number falls between 2 to 5 inclusively of 2 and 5: (num>=2) and (num<=5), and print 'Not Weird' if the case satisfies.
3. Similarly we will do this for 6 to 20 block, but this time we will print 'Weird'  

  • Arithmetic Operators

Python supports a verity of arithmetic operators like   +,  - , * etc., which we will use in this program.

For the program to run smoothly it have to take two int type inputs, which could be named 'a' and 'b' (These are not strings, '' are added for the purpose of highlighting the variables in normal language.). Then we will check for constrains in the sizes of the numbers using if - else, both the upper limit and the lower limit can be checked in the same line of code using the 'and' operator. Once the inputs pass this check we can evaluate  their sum (using '+'), difference (using '-') and finally the product (using '*'), it is important to keep in mind, that while calculating the difference we should put the first input first, which implies we will calculate difference as a-b and not b-a. Each of these values can be either printed directly of stored in variables and print later.

  • Loops

This problem requires the user to print square of whole numbers up-to the limit given in the input. The program starts by taking int type input, and checking the constraints using if - else. Next using a for loop and keeping the range of the loop (number of times the loop is going to execute, is given by its range) equal to the input number. The loop body squares the number (using '**2') and directly prints it and don't store it in any variable.  

  • Print Function

Print function requires us to print a single string (it is noteworthy that it could also be printed as a int type). One can start by taking int type input (because we will use it in range function which don't takes any other type as arguments), and creating an empty sting v='', to which we will join digits to get our final output.

The for loop will iterate (repeat) the number of times our input value and each time we will add our iterating variable (after converting it to string type using str()) to 'v'. After the loop is done executing completely program will print v (this can either be printed as int or str type).



If any reader have any quires or want solution to any problem discussed above, he/she can contact the writer.