Saturday, October 27, 2018

List in python

In order to store a set of variable, we use a list.

A list can be illustrated as a box queue in a line, and we add our value to each box.

The box can be accessed using the box position. We would call it index.

In python index should start from 0.

So, first element is in index 0, second element is in index 1.. and so on.

We add an element to a list by .append(new value). myList.append("newValue")

We will be able to access an element by []

Let us have a list name myList, to get the first element, we will use myList[], second element, myList[1] and so on.

To remove an element from a list, use .pop(index).
To remove the first element, I use myList.pop(0)
To get the first element of a list and remove it, I use removelement = myList.pop(0)

Please do refer to the example below for better understanding on how to add element to a list , access the element of list. remove an element from a list.

There is another question regarding list.
Encyclopedia question


Maximum / Minimum / Average with python

Programming help us to solve problem. Sometimes, we do need statistics to help us making decision.

For instance, we would like to decide which age range of customers prefer books, games or clothes.
We may also need statistics to show our sales, how popular is our movie produce, how is the  customers response to our food and etc.

It helps us to analyze large data sets. There are functions help us to do job easier and faster. But how?

According to the following example, we use max() , min(), function to get the maximum or minimum number. In order to get the average, do use a for loop to sum up the value and calculate the average

I use .rstrip() to trim the empty string of the input string. 

Please do refer to the link below for example.

dishin problem

Sunday, October 21, 2018

How to use loop to analyze large data sets?

If we have a set of data {1, 3, 5, 6, 10, 12}, this should be no problem to analyze.
We may sum it up to get their total, or calculate their average , get their maximum, minimum or mode during one glance.

However, what about we have datasets with 100 data, or up to 1000 or even much more?
Can we calculate the amount manually?

That is why we use computer to solve our daily problem.
We need to shorten our time to analyze large amount of data.

In order to analyze the data, we use loop to access the data one by one.
For sure, we need stopping criteria to tell the compiler when it should stop the execution,
else we might get run time error (the computer keep repeat the steps and do not know when to stop).

I will be demonstrating how loop helps us by using the sample question from Australian Informatics Olympiad programme sample questions. You may registered and get the training from their training site here Australian Mathematics Trust.

According to the question, we need to access the input data from 'rainin.txt' and output the result to 'rainout.txt'. From the input file, the first line give number of days n and capacity of water tanks in liters c. The remaining n lines shows the amount of rain in litres for the n days.

Here comes the question : What is the number of days for the tank to be filled up?

In this question, you do not need to check or validate the data. The main purpose of the program is to calculate number of days to fill up the tank instead of doing validation on the input data.

So, please refer to the pseudocode below for the steps.

Pseudocode:
1.  Open the input file and assign the data to a list. Remember to close the file.
2.  Obtain number of days n, capacity of water tank c from the first element of the list.
3.  Delete the first element of the list, since we do not need it in the list anymore. We had assigned the data of n and c in variables previously
4. I will be using the for loop to access all the element in the list.
5. During each loop, I sum up the water filled in the tank. I increment the number of days need to fill up the tank.
6. Then, I check if the total amount of water filled in the tank reach or more than the capacity. If so break and exit the loop, else repeat the steps until we get the desired amount of water.
7. Finally, open a output file and write the output string to the file. Close the file.

See, the question might seems difficult and confusing intitially. It become easier when we understand the requirement and break down to smaller part. We might get the result.

The next item we need to worry about is the syntax. No worry, there are resources regarding the language syntax online and it is free! Practice makes perfect. Please do refer the link below for the solution

How to solve drought problem

Friday, October 19, 2018

Python week 2

After we learn the basic syntax of python, we need to proceed to variable.

Variable is very important in programming, since it "acts" like a container to store our value.

The type of variables are integer int, string str and float (for decimal number).

You are an elite if you can conquer the confusion of the data type and the casting and conversion.

Assignment a value into a variable help to store the value we need. So, how to access or retrieve the data is also another challenge. It helps us to proceed to next steps we need to do like doing operation or pass into another function for further action.

I had include the notes, exercise regarding the datatype, and how to access it in the link below.

Have fun!

Python notes_week 2

Code with python - first class

The name of computer is named by the word "compute".

Basically the role of computer cannot be fulfilled without compute.

Okay. Lets make it simple and see how computer compute?

Let's start with the basic.

I would like to introduce coding using language python.

Python is a interpreted, object-oriented, high-level programming language with dynamic semantics.

Among the language I had approached like C#, C++, Java and Python,
I would like to introduce Python for beginner.

In terms of syntax, I consider Python as easier and simpler language for beginner during my first approach to programming.

Furthermore, I suggest Python especially for the use in Mathematics.

I love the package useful for Mathematics that available in Python like Scipy, Pypi, Numpy, Matplotlib and so on.

I had attached the notes on the basic of python in the link below.

Why not trying?  Python First Week Class

Saturday, October 13, 2018

Arithmetic progression

Extra questions regarding arithmetic progression.

Please try!

Arithmetic progression

Numbers

There are numbers. 

Types of numbers can be categorizes as :
  • Natural numbers
  • Positive numbers
  • Negative numbers
  • Prime numbers
  • Rational numbers 
  • Irrational numbers
  • and so on
Please do refer to the attached pdf file below for further information.
Numbers

Thursday, October 4, 2018

How while loop works

This question focus on the understanding regarding the principle of while loop.

while loop is repeating the step under it until you cannot fulfill the condition

Python code 
inputNo = 10
i = 1
while (i <= inputNo):
    outputFile.write(str(i) + '\n')
    i = i + 1

Example
inputNo = 10 : Assign/set 10 to inputNo (inputNo is the name that store value 10)
i = 1 : Assign / set 1 to i (i is the name that store value 1)
while (i <= inputNo): 
means that while you can fulfill the condition in the brackets , that's mean you can find i that is smaller or equal to the inputNo

now i = 1, which is smaller or equal to 10
so do the actions under it 

write i to outputFile ('\n' means enter to the next line)
then i = i + 1 : means we add 1 to i, i is become 2 now

Again while (i <= inputNo): now i = 2 which is smaller than 10
then write 2 to output file
i = 2 + 1 = 3

Now i = 3, i is smaller than 10
write 3 to output file
i = 3 + 1 = 4

Now i = 4, i is smaller than 10
write 4 to output file
i = 4 + 1 = 5


...... (repeat the step)

Now i = 8, i is smaller than 10
write 8 to output file
i = 8 + 1 = 9

Now i = 9, i is smaller than 10
write 9 to output file
i = 9 + 1 = 10 

Now i = 10, i is equal to 10, fulfill the condition too
write 10 to output file
i = 10 + 1 = 11

Now i = 11, i is not smaller than 10, i is not equal to 10
we cannot fulfill the condition.
So, break the loop. We will exit the loop which means we will not repeat the action under the loop.


Counting question

Tak Tak Fruit Problem

This is a question that challenge your understand skill and programming skill.

The Tremendous Tak-Tak Tree
Output File: taktakout.txt
Time Limit: 1 second
Input
Output
Input File: taktakin.txt
Life is simple in the isolated village of Tak-Tak. Free of the noise and bustle of modern life, the villagers spend their days indulging their two great loves: farming and cooking. Their vegetable fields grow strong and tall, and their delicious communal meals brighten many an evening.
In the centre of the village sits a great sprawling tree, whose long boughs provide shade during the hot summers. The fruit of the Tak-Tak tree (as it is affectionately known) is renowned for its sweet, succulent taste.
Unlike normal plants, the Tak-Tak tree is not affected by weather or seasons. Instead, its fruits grow according to the cycles of the moon. Every month when the full moon is at its highest, each fruit on the tree shimmers and turns into two fruits. When the sun rises the villagers wake to find the number of fruits on the tree has doubled.
The villagers are planning a huge feast for this year. On the day of the feast they will remove all the fruit except onefrom the tree. This is to be divided equally between the village's eleven inhabitants, as they are a fair people. (No slicing or dicing is allowed - each villager must receive a whole number of fruits.) Afterwards the one remaining fruit on the Tak-Tak tree is left to begin the cycle anew.
If the villagers cannot evenly share the fruit with one left over, they shall wait until they can. They are very excited about the feast and would like to know how many full moons they must wait.
The input file will consist of a single integer, the number of fruits initially on the tree. This will be between 2 and 1,000,000 inclusive. 
The output file should consist of two integers separated by a space: the least number of full moons the villagers must wait until the feast is possible, and the number of fruits on the tree at that time.
You are guaranteed that it will eventually be possible to hold the feast.

To solve this question:
1. List down the condition given:

  • the number of fruit will be doubled each day
  • must leave exactly one fruit during the harvest day
  • there are 11 inhabitants to share the fruit evenly

2. The input give you the number of fruits on the first day.
3. You need to find out the number of days so that you can harvest and 1 fruit will be left.
4. Build a mathematics formula

  • number of fruits after first day = number of fruits X 2   
    • to check if harvest is allowed : number of fruits after first day / 11 = quotient R 1
    • if not then we need to check for next day


  • number of fruits after second day = number of fruits after first day X 2
  • 5. Do we really need to calculate by our hand until we get the answer? No worry . We might help ourselves by using a customized application / program

     The ways :
    1. Get the input file value - number of fruits on the first day (name it as number of fruits)
    2. we need a looping (what is a looping?? - we repeat the calculation with same formula until we get the anser)
    3. we know that number of inhabitants = 11

    • It could be easy if the answer if 0 day
    • 23 - 23 / 11 = 2 R 1
    • then number of day = 0, total fruit = 23
    • we might not be lucky 

    4. At first number of days = 0
    4. When we find out that

    • if the number of fruits / number of inhabitants = Quotient R 1 
      •  return number of days
      • return number of fruits
    • else
      • number of fruits = number of fruits X 2
      • number of days = number of days  + 1

     Pseudocode (we use english language to plan our program):
    1. get input the number of fruits
    2. set number of inhabitant = 11
    3. set number of days = 0
    4. when number of fruits % number of inhatant != 1
    (!= means not equal to ) (% means modulus - computer the remainder)

    • number of fruits = number of fruits X 2
    • number of days ++ 
    5. Then repeat the no.4 until number of fruits % number of inhatant == 1 (== means equal to )
    6. print the value to output file

    Please refer to the python code below to get the answer: 


    Tak Tak Problem Python Code

    Wednesday, October 3, 2018

    Mind Bending problem


    A Mindbending Scenario

    Input File: bendin.txt
    Output File: bendout.txt
    Time Limit: 0.05 seconds
    Imagine a universe far different to the one we know. One where where people can walk through walls, where politicians don't lie, where the rules of physics itself are bent on a daily basis. Imagine how strange it would be to land in that universe, having to adjust to entirely new laws of reality, and having to integrate yourself into sentient cultures far beyond human comprehension.
    Now, imagine that everything in this universe was suddenly destroyed in a cataclysmic explosion, leaving nothing but two rectangles on an endless plane. These two rectangles have their corners at integer co-ordinates (rational numbers having been destroyed). Your task is to discover the total area of the plane which is covered by these rectangles.
    Beware! Some parts of the plane may be covered by both rectangles. If you merely add the individual areas together, you are surely doomed to incorrect answers!

    Input

    The input file will consist of two lines. Each line will be of the form x1 y1 x2 y2, describing the bottom-left and top-right corners of one rectangle. You are guaranteed that 0 <= x1 < x2 <= 10,000, and 0 <= y1 < y2 <= 10,000. (That is, each rectangle is at least one unit square in size.)

    Output

    Your output file should consist of a single integer: the total area covered by rectangles. (Remember - the rectangles may overlap!)
    Please check on the python code by clicking the link below:  Mind bending problem python code
    You may get the original question from Mind bending problem question

    Monday, October 1, 2018

    Mixed function with python

    Mixed Fraction

    Input File: mixin.txt
    Output File: mixout.txt
    Time Limit: 1 second
    You are sitting at your computer surfing the internet when a chance forum post stirs memories of an idyllic past. There were so many different ways of representing rational numbers back then - percentages, decimals, and of course the mixed fraction. There was always something so wonderful about mixed fractions - the way they quickly told you how big the number was yet still conveyed the subtleties of the fractional part. Such elegance... You shed a silent tear for days long gone.
    In this task you are given a fraction in the form n/d, where 1 <= d < n <= 1,000,000,000. Your task is to find the two integers a and b, where 0 <= b < d and ad + b = n. You do not need to (and shouldn't) simplify the fraction.

    Input

    A single line containing the integers n and d separated by a space.

    Output

    If b is not 0, print a single line in the format a b/d. Otherwise, print a.

    Please click on the python code below:

    Superior of partial fraction

    What is complex fraction? Complex fraction is a fraction whereby its numerator or denominator or both consists of fractions. Partial frac...