Last active
April 29, 2021 15:23
-
-
Save rapando/606593cd44be747b751a27e743ce096f to your computer and use it in GitHub Desktop.
Habiba Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
/* | |
Recap: | |
In formatted string, | |
%d stands for integers | |
%s strings | |
%b boolean (this is for Go) | |
%f float | |
Arithmetics | |
----------- | |
1. Addition | |
2. Subtraction | |
3. Multiplication | |
4. Division | |
5. Modulus | |
6. Increment (pre-increment and post-increment) ** go does not have pre | |
7. Decrement (pre-decrement and post-decrement) | |
-- If you do any of these operations to values with the same data type, the answer will be of the same data type | |
-- To avoid confusion, always use values of the same data type | |
-- If you have to use different data type, cast the convenient one | |
-- If you cast 4 to a float, it will be 4.0 | |
-- Compatible casting | |
1. Float and Integer (it is prefered to cast integers to floats, not the other way round. to avoid losing data) | |
2. Strings and floats/integers (casting strings to integers/float, the string has to contain valid numbers only) | |
e.g you can cast "45" to 45 or "54.32" to 54.32 but you cannot cast 45x | |
- note that, in Go there are special functions to convert between integers/floats and strings | |
- *** Read on : - fmt.Sprint, fmt.Sprintf (integers to strigns), strconv.Atoi, strconv.ParseFloat (strings to integers/floats to strings) | |
*/ | |
// 1. Addition | |
var a = 65 | |
var b = 43 | |
var c = a + b | |
fmt.Println("a + b = ", c) | |
fmt.Printf("4 + 7 = %d\n", 4+7) | |
var s1 = "No" | |
var s2 = "Yes" | |
var s = s1 + s2 | |
fmt.Printf("adding strings : %s\n", s) | |
// this is the same for subtraction, multiplication and division | |
// a * b | |
// a - b | |
// a / b | |
// Modulus gives the remainder of a division. Mostly used for divisibility tests (if the answer is 0 in a % b, a is divisible by b) | |
// e.g | |
var j = 102 | |
var k = 3 | |
var i = j % k | |
fmt.Println("j % k =", i) | |
// shorthand for these | |
// a = a + b (a += b) | |
// a = a * b (a *= b) | |
// a = a - b (a -= b) note that a = b - a remains that way. | |
// a = a / b (a /= b) | |
/* | |
numberOfPeople := 50 | |
newPeople := 10 | |
numberOfPeople = numberOfPeople + newPeople // numberOfPeople += newPeople | |
*/ | |
// Increment | |
// post-increment | |
a = 10 | |
b = 10 | |
fmt.Println("The value of a + 1 is", a+1) | |
fmt.Println("The value of a is", a) | |
a++ | |
fmt.Println("The value of a is", a) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
How to compile | |
gcc -o increment increment.c | |
*/ | |
int main() { | |
int x = 1; | |
// pre-increment increments the value before being used | |
printf("x is %d\n", x); | |
printf("pre-increment: x is %d\n", ++x); | |
printf("x is %d\n", x); | |
// post-increment: increments the value after being used | |
x = 1; | |
printf("x is %d\n", x); | |
printf("post-increment: x is %d\n", x++); | |
printf("x is %d\n", x); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
var alive bool | |
alive = false | |
if alive { | |
fmt.Println("You are alive") | |
} else { | |
fmt.Println("You are dead") | |
} | |
/* | |
for other data types, check if the value is equal to or not equal to | |
what you are checking | |
= assigning operator | |
------- | |
== is equivalent? | |
!= is not equivalent? | |
These two work for every data type | |
Example. 2 is equivalent to 2, 3 is not equivalent to 2 | |
> greater than? | |
<= less than or equal to? | |
Example, we need to check against 3. | |
options : 1,2,3,4,5,6 | |
Numbers that are greater than 3 are {4,5,6} | |
Numbers that less than or equal to 3 are {3,2,1} | |
< less than? | |
>= greater than or equal to | |
Example, we need to check against 3. | |
options : 1,2,3,4,5,6 | |
Numbers that are less than 3 are {1,2} | |
Numbers that greater than or equal to 3 are {3,4,5,6} | |
===== | |
Assuming you want 3 to appear in both cases, it will take two conditions | |
Numbers that less than or equal to 3 are {3,2,1} | |
Numbers that greater than or equal to 3 are {3,4,5,6} | |
---- NB ---- | |
We always compare the | |
*/ | |
var age int | |
age = 18 | |
if age < 18 { | |
fmt.Println("Kwenda") | |
} else { | |
fmt.Println("Karibu") | |
} | |
/* | |
[if] If your age is below 18, deny service <18 | |
[else if] If your age is 18-55 inclusive allow service | |
[else] If your age is above 55, refer to another place | |
*/ | |
age = 65 | |
if age < 18 { | |
fmt.Println("Kwenda") | |
} else if age <= 55 { | |
fmt.Println("Karibu") | |
} else { | |
fmt.Println("You are referred") | |
} | |
/* | |
Nesting is where you have decisions inside decisions, e.g | |
if gender == "female" { | |
if age < 18 { | |
// teen girl | |
} else { | |
// woman | |
} | |
} | |
an alternative to check at once | |
if gender == "female" and age < 18 | |
if gender == "female" and age >= 18 | |
Habiba is a female who is 25 years old who is Ugandan | |
Conditions : A. gender == "female", B. age < 18, C. Nationality == "kenyan" | |
A = true, B = false, C = False | |
A and B = false | |
A or B = true | |
When you have both AND and OR, | |
A and B or C | |
- [A and B] or C (most likely) = [A and B] = False, False or False = False | |
- A and [B or C] = [B or C] = False, A and False = False | |
It is advisable to group relevant conditions together | |
e.g | |
if (A and B) or C | |
if A and (B or C) | |
------ | |
Symbols: | |
AND : && | |
OR : || | |
*/ | |
/* | |
For males: | |
below 18 - Hi boy, 18 and above: Hi man | |
For females: | |
below 18 - Hi girl, 18 and above: Hi woman | |
This has two approaches | |
if gender == "female" { | |
if age < 18 { | |
fmt.Println("Hi girl") | |
} else { | |
fmt.Println("Hi woman") | |
} | |
} else { | |
if age < 18 { | |
fmt.Println("Hi boy") | |
} else { | |
fmt.Println("Hi man") | |
} | |
} | |
Let's say we own a strip club and we are only employing females who are 18 and above | |
*/ | |
var gender = "male" | |
age = 30 | |
if gender == "female" && age >= 18 { | |
fmt.Println("You are employed") | |
} else { | |
fmt.Println("Go away, you don't qualify") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
/* | |
Switch is an alternative to if...else | |
Switch is used in cases of == | |
Switch does not support AND, OR meaning, you have to check only one condition at a time | |
The switch equivalent of this: | |
if gender == "female" { | |
fmt.Println("Hi woman") | |
} else if gender == "male" { | |
fmt.Println("Hi man") | |
} else { | |
fmt.Println("Hi transgender") | |
} | |
*/ | |
var gender = "female" | |
switch gender { | |
case "female": | |
fmt.Println("Hi woman") | |
break | |
case "male": | |
fmt.Println("Hi man") | |
break | |
default: | |
fmt.Println("Hi transgender") | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- In all questions below, you can use either switch or if..else | |
--------------------------------------------------------------- | |
1. You have been given these conditions (Concider that there are only two genders: male and female): | |
- If a person is male and above 18 but 35 and below, print this statement. "You are ready for war" | |
- If a person is male and above 35, print this statement: "You are ready for children" | |
- If a person is female, print this statement: "Welcome lady" | |
2. A certain company used the following table to determine their employees salary | |
position yearsInService salary | |
manager 0-23 8000 | |
manager 24 to 45 10000 | |
janitor < 5 40 | |
janitor 5 to 10 100 | |
janitor > 10 1000 | |
Write if...else/ switch statements that take care of all the above scenarios. | |
Also, if John is a janitor who has been working for 18 years, print his salary | |
3. Given the following ages {10, 20, 30, 40, 50, 60 }. | |
Jane wants to award people who award people who are 40 and 50. | |
She wants to punish people who are 10 and 20. | |
People who are 30 will not be awarded or punished. | |
Write if..else/ switch statements that will print the action to be taken for the respective age groups. | |
*/ | |
package main | |
func main() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
/* | |
Loops (repetition) is used to run the same multiple times. | |
In programming there are types of loops: | |
1. For loops | |
2. While loops | |
3. Do...while loops | |
For loops and While loops are used to repeat the same code 0 to infinite times. | |
Do while loops are used to do once and then repeat 0 to infinite times. | |
Go does not have while, so you can use for in its place. | |
-- When looping you need something that will tell you when to stop. Usually, this is a counter. | |
*/ | |
// 1. Looping ten times | |
var i int | |
for i = 1; i <= 10; i++ { | |
fmt.Println("Hello world ", i) | |
} | |
fmt.Println() | |
for i = 10; i > 0; i-- { | |
fmt.Println("Reducing : ", i) | |
} | |
/* | |
While loops are commonly used when you don't know how many times you want to loop. | |
A sample looks like this | |
int count = 0 | |
while count < 10 { | |
print (count) | |
count++ | |
} | |
*/ | |
// Go does not have while loops, instead it allows you to use for | |
i = 0 | |
for i < 10 { | |
fmt.Println("while... ", i) | |
i++ | |
} | |
// infinite loops | |
/* | |
1. You can use conditions that never fail | |
e.g (while loop) | |
while true {} | |
while 1 == 1 { | |
} | |
for true { // go alternative | |
} | |
NB : for infinite loops use while or | |
2. for ;; { // c, java, other languages | |
} | |
for { // in Go | |
} | |
*/ | |
/** | |
Sometimes you want to stop the loop before the number of repetions end. | |
In that case we use break | |
**/ | |
// loop 10 times, but when the value of the counter (i) is 5, stop | |
for i = 0; i < 10; i++ { | |
if i == 5 { | |
fmt.Println("we've reached 5") | |
break | |
} | |
fmt.Println("break -- ", i) | |
} | |
/** | |
Sometimes we want to skip a step, in that case we use continue | |
**/ | |
// loop 10 times, but when the value of the counter (i) is 5, ignore that step | |
for i = 1; i <= 10; i++ { | |
if i == 5 { | |
fmt.Println("we've reached 5") | |
continue | |
} | |
fmt.Println("continue -- ", i) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = 0 | |
# normal while loop for looping ten times | |
while i < 10: | |
print ("hello world ", i) | |
i += 1 | |
# usually while loops are used for situations when you don't know how many times you are going to loop | |
""" | |
transaction_status = 'pending' | |
while transaction_status is 'pending': | |
print ("retry") | |
""" | |
# do ... while | |
# in do...while, the code runs at least once before the condition is checked. | |
# python does not have a do...while loop but we can use while | |
i =0 | |
while True: | |
print("do while ... hello world") | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
To compile | |
gcc -o dowhile dowhile.c | |
*/ | |
int main() { | |
/* | |
do while loop will run at least once before checking the condition. | |
*/ | |
int i = 0; | |
do { | |
printf("hello world %d\n", i); | |
i+=2; | |
} while (i < 10); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
/* | |
Recursion, | |
Warning : don't use it unless you have to | |
-- Getting fibonacci of a given index | |
0, 1, 1, 2, 3, 5, 8,11...etc | |
*/ | |
now := time.Now() | |
var a = 45 | |
var f = fibonacci(a) | |
fmt.Printf("The fibonacci of index %d is %d . Took %v\n", a, f, time.Since(now)) | |
now = time.Now() | |
a = 45 | |
f = fibonacci2(a) | |
fmt.Printf("The fibonacci of index %d is %d . Took %v\n", a, f, time.Since(now)) | |
} | |
func fibonacci(index int) int { | |
if index <= 1 { | |
return index | |
} | |
return fibonacci(index-1) + fibonacci(index-2) | |
} | |
// Alternative that does not use recursion | |
func fibonacci2(index int) int { | |
if index <= 1 { | |
return index | |
} | |
var a = 0 | |
var b = 1 | |
var f int | |
for i := 2; i <= index; i++ { | |
f = a + b | |
a = b | |
b = f | |
} | |
return f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment