[8 golds!] I wannabe programmer

Day 2,588, 14:37 Published in Egypt Spain by etchner

Hi guys.

The last contest was related to History and society. Now, we're going to do something completely different. We're going to be programmers!



If you're programming, you tell your computer to do a list of ordered instructions, that is called "Algorithm".

So imagine that you're a programmer, an expert in a programming language called "ETCH++" which has these instructions:

ASSIGN VALUE value
SUM
SUB
DIV value
MULT value
GET VALUE AND ASSIGN
WHILE() END
GREATER
LOWER
EQUAL
NO_EMPTY


Ok, I'm gonna explain more about each instruction.

ASSIGN VALUE val

You create a variable, an element which a concrete name and a value which can change during the algorithm. Example

ASSIGN freedomForEgypt VALUE 5

Now I have an element called "freedomForEgypt" whose value is 5

ASSIGN hiMikhail VALUE 60

Now I have an element called "hiMikhail" whose value is 60

And I can do

ASSIGN etch VALUE freedomForEgypt

I get an element called "etch" whose value is 5 (because freedomForEgypt's value is 5)



SUM

Sum value to and save the result in


ASSIGN freedomForEgypt VALUE 5
ASSIGN hiMikhail VALUE 60
SUM freedomForEgypt hiMikhail

now, freedomForEgypt value is 65


SUB

Substract value of and save the result in


DIV

divide by and save the result in

MULT

multiplicate by and save the result in

GET VALUE AND ASSIGN

Ok, this is important. Imagine there's an place in your computer memory with a lot of numbers, this instruction get one value from your memory, and move the value to a new variable,

Memory: 5,3,6

GET VALUE AND ASSIGN etch

Now, we have two numbers in the memory (3,6) and a variable, called "etch" whose value is 5

GET VALUE AND ASSIGN ahmed
GET VALUE AND ASSIGN enrivax

Now, we have no numbers in memory and we have three variables (etch = 5, ahmed = 3 and enrivax = 6)

WHILE() END

This is a loop, a list of instructions that will execute while is true. We have 4 conditions:


GREATER THAN
LOWER THAN
EQUAL AS
NO_EMPTY


For example,

ASSIGN freedomForEgypt VALUE 5
ASSIGN hiMikhail VALUE 2

WHILE (LOWER hiMikhail THAN freedomForEgypt)
SUB freedomForEgypt 1
END

We've created two variables (freedomForEgypt = 5, hiMikhail = 2) and while freedomForEgypt is greater than hiMikhail, we substract 1 to freedomForEgypt

Before the loop:
freedomForEgypt = 5
hiMikhail = 2

After first iteration of the loop:

freedomForEgypt = 4
hiMikhail = 2

After secon😛

freedomForEgypt = 3
hiMikhail = 2

After thir😛

freedomForEgypt = 2
hiMikhail = 2

freedomForEgypt is no longer greater than hiMikhail, so WHILE stops


The condition NO_EMPTY is true while your computer memory have any number to get.


OK, this is a little example

ASSIGN result VALUE 0
WHILE (NO_EMPTY)
GET VALUE AND ASSIGN temporal_variable
SUM result temporal_variable

END

This program create a variable, called result, and calculate the sum of all elements in your computer's memory. If my memory have these numbers: 5, 6, 7,8,9,10 result will be 45 (5 + 6 + 8 + 9 + 10 = 45)


Now is your turn, you have to solve three problems:

1) I give you these initial instructions:

ASSIGN needle VALUE 56
ASSIGN stack VALUE 560

And I want that you continue the algorithm to swap the value of both variables. I want that needle will be equal to 560 and stack will be 56. And your algorith should be valid for any hypothetical value of both variables.


2) You have a list of numbers in your memory, I want you to calculate the mean of these values. (sum of a list of numbers divided by the size of the list):


3) Now you know how to calculate the mean of a list of numbes. I want you to calculate the mean (yes, again) and then, variance (http://en.wikipedia.org/wiki/Variance).


Hint: You can calculate variance of a list of numbers getting the sum of square of all these numbers. Dividing the sum by the lenght of the list and then substracting the square of mean to new result






Sounds hard? You've until the last day of the year to solve these problems.

You've to post your solutions in that form: http://goo.gl/forms/3nBk5eySwS

I will give 8 golds as prizes. The prize will be shared between all correct answers.

GOOD LUCK