25/10/13 – Python Challenges (Most Efficient Solutions in the Class!)

This was the last lesson before of the half term, and we were given a set of challenges to do in our given groups. The only way we could progress on to the next challenge was by completing the first. The winners would be the ones with the most challenges completed by the end of the lesson.

My group consisted of Myself, Zain, Mithunan and Arjun, and although our evidence is to be posted on separate blogs (and it is), I have posted my own copy o it on my as well. We finished 3 challenges:

1. We see 32 written slightly above 2. Our first guess was to type up 238 in the URL, however we were told to try something else by an error site which was eventually opened. So we eventually figured out the the reason for the 38 being above the 2 was because we wanted to find the 38th power of 2. So we figured out the answer to 2 ** 38 (274877906944) and entered it in the URL, which took us to the next challenge.

Screen Shot 2013-10-27 at 11.59.40

Screen Shot 2013-10-27 at 12.00.35

2. The next challenge involved cracking a code. The hint suggested we think TWICE, so we decided to translate the code given by going 2 letters higher each time. After entering this into the URL we realised it was’t the correct answer. So we looked again, and saw that the URL had the word ‘map’ in it. We went 2 letter higher on each letter of ‘map’ (‘ocr’), and we were taken on to the next challenge.

Screen Shot 2013-10-27 at 12.04.29Screen Shot 2013-10-27 at 12.04.41

To help with the efficiency of solving this challenge, I created a program in python which would do this for us:

Screen Shot 2013-10-29 at 23.25.17

 

This program refers each letter in a word to a dictionary and then increase the index of each letter in the word by 2, and refers the new index to the dictionary.

The output is as follows:

Screen Shot 2013-10-29 at 23.25.45

3. This challenge told us to look at the SOURCE of the page. I knew this was referring to the source code of the page. Here was a comment with a mess of random characters, and were told to find the rare characters in it. To my knowledge all the groups who attempted this challenge used the CONTROL+F browser function which looked up characters on the page. The word was ‘EQUALITY’.

Screen Shot 2013-10-27 at 12.33.19Screen Shot 2013-10-27 at 12.12.53Screen Shot 2013-10-27 at 12.37.12

However, I felt this method to be greatly inefficient, and I made a python program for this as well. This takes in the large string as input, and if the characters in the string were not in the alphabet (using the NOT IN function), we append the to a list, if they are in the alphabet we append them to another list. After iterating through the list we print out the list with the word in the alphabet which have been picked up. This give us the output of  [‘e’, ‘q’, ‘u’, ‘a’, ‘l’, ‘i’, ‘t’, ‘y’]. All the steps involved in my program are shown below:

1.Screen Shot 2013-10-27 at 12.35.11

2.Screen Shot 2013-10-27 at 12.38.283.Screen Shot 2013-10-27 at 12.38.49

18/10/13 – Decimal to Binary to Hexadecimal

Eventually Rohit and I finished our Denary to Binary converter. We also added another aspect to our code by which we convert the denary number to an equivalent Hexadecimal Value.

What is Hexadecimal?

Hexadecimal is a BASE 16 coding language, which is used alongside binary in the running of the computer. We see it used as a more user-friendly way of representing binary code, as it simplifies 4 binary bits to being 1 hexadecimal bit. We see Decimal Number being converted to Hexadecimal as shown in the table below (with all number from 1 – 10 the same in both Hex and Decimal, but numbers after 10 to 16 converting to letters from A to F).

670px-Convert-Hexadecimal-to-Binary-or-Decimal-Step-1

So how do we convert binary number to a hexadecimal:

  1. We split the binary number into groups of 4 (as 1 Hex bit is 4 Binary bits)
  2.  If any of the groups of 4 do not have enough numbers in them, we add on extra 0s to the beginning of the list without 4 bits in it (This process is shown below)629px-Convert-Binary-to-Hexadecimal-Step-3
  3. From here there are 2 ways to go about doing this:
  • We can either go through each group of 4 and convert them to their equivalent Hex counterpart, and if the Binary value is greater than 10 we can refer it to a dictionary.
  • Or we can find the Decimal form of each Binary Value and then divide it by 16 (due to Hex being a Base 16 number system). If they divide exactly then we base the first Hex number on the amount they divide by, and apply the same rule to its remainder.

Our Code

To solve this problem we began by making a simple piece of code to convert a Decimal Number to Binary:

Screen Shot 2013-10-20 at 18.00.45

 

To further convert the values to Hexadecimal we first had to make a dictionary with Hex Values from 10 to 16:

Screen Shot 2013-10-20 at 18.04.41

 

Finally we solved the problem using both logics explained above (and adding some extra variables – don’t worry if you don’t get it, I have posted the all of our work on my Google Sites page).

Method 1:

Screen Shot 2013-10-20 at 18.08.46

 

Method 2:

Screen Shot 2013-10-20 at 18.08.12

09/10/13 – Denary to Binary and Hexadecimal

Today we were given the task of converting a binary number to Denary. The initial task required us to print a number which only went up to 8 bits in equivalent Binary 1s and 0s. However Rohit and I felt this was too limiting and made the program pretty useless. So we decided to find a way of doing the same problem only being able to use any digits to get their binary equivalent.

When attempting to do this task in SCRATCH we were able to find some key issues with Scratch programming to understand how to run a code. As if you have prior programming knowledge you find it difficult to interpret the block of functions into more difficult functions and coding abilities. Therefore we decided to begin by doing the in python. The task was more difficult than we thought it would be, and took us 3 hours to make a Denary to Binary converter using logic functions of If, Else and Elif. Although we were aware of a much simpler method of Diving the binary number by its remainder when its divided by 2.

We have finished the Denary to Binary calculator part, however we are yet to complete the Denary to Hexadecimal part. We also intend to do alter our calculator further to allow it to convert Binary and Hexadecimal back to Denary. Using python graphics module, Rohit and I plan to put all of this into a console based system which will involve number entry like a calculator. Finally, we will interpret this all into Scratch, and I will post it on my blog.

However if you wish to see my Binary to Denary Calculator, feel free to ask me.

04/10/13 – Class Challenge

This week we did a class challenge to test our knowledge of what we had learnt over the past few weeks. The booklets consisted of a variety of logical thinking problems which we we have to solve using our knowledge of logic gates and problem solving. There were also some tasks on binary.

The challenge culminated in the making of a scratch program for a car park.

So in the case of my Scratch program (which I made with the help of my good friend Rohit), we have found a way of simulating a car park. So to begin with the car park randomly selects a number between 1 to 10, to tell us how many spaces it has in it. In a Forever loop, you are asked if you “Wish to Enter”, and if “The ticket has been issued,” and if the answer to both of them is “Yes” the barrier will open.

The most interesting bit about our Scratch program is after this, as we that when the barrier has opened, the car park loses 1 space. When the spaces in the car park eventually become 0, you are asked if “You wish to try another car park.” If the answer to this final question is no, the program stops, if the answer is Yes, the the program will randomly generate another number of space to simulate another car park, and then restart the loop!

Image

This wasn’t too complicated, so we decided to try to replicate this program in python using the same basic logic. Here is what we got:

 

27/09/13 – Lesson 7 Advance Logic Gates

This week, after understanding how basic logic gates work, and looking at their truth tables, I moved on to understanding more complex gates.
The NAND Gate
The NAND Gate, a mixture of the Not and And Gate, is basically the opposite of the And gate, in which when the bits of 0 are collected, they produce and output of 1.

The NOR Gate
The NOR Gate, a mixture of the Not and Or gate, makes the output 1 when neither values are on, and makes it zero when either value A OR value B is on.
04129

The Xor Gate

This is an Exculsive Nor gate, meaning it has opposite or alternating properties to its counterpart. Similar to an OR gate but the output is only high if only one of the two inputs is high.

Xor

After exploring these slightly more difficult logic gates, and understanding how they could be incorporated in a larger circuit with more than just the inputs A and B, we tried some challenges.
Exercise 1:
“A car Radiator cooling fan must only switch ON when the ignition switch is ON (ignition OFF = 0, ignition ON = 1) AND the water temperature is high (Low temp = 0, High Temp = 1) “
My solution:
Ex 1
We represented the ignition and the water temperature as being 2 switches, and put them through and AND gate. The output of the Radiator Cooling Fan is the light which shine.
Exercise 2:
“The Car designer wishes to be able to sound an Alarm if the driver tries to leave the car with the lights ON and the ignition OFF. The alarm system will have three sensors, one sensor on the door (0 = door shut, 1 = door open), another sensor on the light switch (1 = ON, 0 = OFF), and another sensor on the ignition switch (1 = ON, 0 = OFF). Design a logic circuit which will provide the car designer with a solution to this problem.”
My solution:
Ex 3

20/09/13 – Lesson 5 Basic Logic Gates

Logic Gates are around us in everything. They can be used metaphorically to explain choices we make, or we can see them in circuits in gadgets around the house. You also have to use logic to make decisions in large groups of code. There are 3 basic logic gates:

AND Gate

And

This gate requires all inputs to be 1, for an output of 1, or else you will get an output of 0. You will only get a fully flowing circuit if all the values are high. This is useful if you need 2 conditions for one thing to happen, like in a car park, for example if the car has to be in the right position and the car park has to have spaces in it for a ticket to come out.

NOT Gate

Not

For this gate if the input is 0 then the output is 1. And if the input is 1 then the output must be 0. As this gate produced a value which is the opposite of what it should be, it gives us NOT values. These are often used in the cases of push-to-break switches, ifthe switch is pushes (1) then the output will be nothing (0), and vice versa. We see this in the case of emergency stop buttons

OR Gate

OR

The OR gate require one or the other input to be 1 for an output of 1. So if one of the other are on you will get the circuit one. An OR gate also works if both values are 1. This can be useful if you have 2 switches controlling one light, in which case if one or the other are on then the circuit should work.

13/09/13 – Lesson 3 Binary and Denary Numbers

When we input a value into the computer, the keyboard will send messages to the processor of the computer in a series of 1s and 0s. And then the processor will put and output on the computer screen. Binary is a Base 2 language, meaning it has a variety of 2 different characters which it accepts, as compared to decimal or denary which uses 10 characters.

Binary to Denary

4 bits of Binary is 1 bit of Hexadecimal. Hexadecimal is a base 16 number system, which is used to simplify large binary code. This makes it easier for a 32-bit binary processor to compute a value, as it is being simplified into lesser bits.

So how would you convert binary to hexadecimal:

Binary-to-Decimal-and-Hexadecimal-Conversion-Memorization-Chart

 

Then how would this apply to numbers and letters in binary specifically:

images

06/09/13 – Lesson 1 Basic Sratch Program

This lesson we began by exploring the usage of Scratch. Now scratch is a software which is used to simulate how coding can be done for programming novices. Now although scratch has its limitation it can be fun to test out code and solve certain problem on. I found that by solving problem on Scratch I could more easily solve them in Python or Javascript.

Screen Shot 2013-10-11 at 08.38.25My basic program made the cat move across the screen and look like it was walking by a change in the image of the sprite after 0.2 seconds of having it move in the direction arrow keys pressed. I found this quite easy as I have used scratch a number of times before.