Library Routines đ©*
Welcome to the world of library routines! These are like secret agents in your codeâquick, efficient, and always ready to do the heavy lifting. From finding remainders to generating random numbers, these routines save the day when the going gets tough. Letâs explore their magical powers and how they translate from pseudo-code to Python. And yes, there might be some cheeky humour ahead. đ
MOD (Remainder of Division)â
Think of MOD as the nosy neighbourâit peeks into your numbers and tells you whatâs left over after division.
Pseudo-Code Example:
Value1 â MOD(10, 3) // Remainder of 10 Ă· 3
Value2 â 10 MOD 4 // Remainder of 10 Ă· 4
Python Translation:
Value1 = 10 % 3 # Remainder of 10 Ă· 3
Value2 = 10 % 4 # Remainder of 10 Ă· 4
Output:
Value1 = 1
Value2 = 2
The MOD operator is perfect for detecting even and odd numbers. If x % 2 == 0, congratulationsâitâs even! Otherwise, itâs a quirky oddball.
Fun Example:
Scenario: Youâre dividing pizza slices among your friends, but thereâs always that one leftover slice.
Slices â 7
Friends â 3
Leftover â Slices MOD Friends
OUTPUT "Leftover slice count: ", Leftover
Python:
Slices = 7
Friends = 3
Leftover = Slices % Friends
print("Leftover slice count:", Leftover)
Output:
Leftover slice count: 1
"Who gets the last slice? The one with the best puppy eyes!" đ¶đ
DIV (Quotient of Division)â
DIV is the no-nonsense accountantâit gives you the whole number part of division, ignoring the messy decimals.
Pseudo-Code Example:
Value1 â DIV(10, 3) // Quotient of 10 Ă· 3
Value2 â 15 DIV 4 // Quotient of 15 Ă· 4
Python Translation:
Value1 = 10 // 3 # Quotient of 10 Ă· 3
Value2 = 15 // 4 # Quotient of 15 Ă· 4
Output:
Value1 = 3
Value2 = 3
DIV doesnât care about the remainder. If youâre expecting decimals, you might feel shortchanged! Use / for proper division.
Fun Example:
Scenario: Youâre splitting chocolates among friends but being strict about fairness.
Chocolates â 10
Friends â 3
EachFriend â Chocolates DIV Friends
OUTPUT "Each friend gets: ", EachFriend
Python:
Chocolates = 10
Friends = 3
EachFriend = Chocolates // Friends
print("Each friend gets:", EachFriend)
Output:
Each friend gets: 3
"The leftover chocolates? Thatâs a bribe for the one counting." đ«đ
ROUND (Rounding to Decimal Places)â
ROUND is the perfectionistâit smooths out those pesky decimals to the desired number of places.
Pseudo-Code Example:
Value1 â ROUND(6.97354, 2) // Round to 2 decimal places
Value2 â ROUND(3.14159, 3) // Round to 3 decimal places
Python Translation:
Value1 = round(6.97354, 2) # Round to 2 decimal places
Value2 = round(3.14159, 3) # Round to 3 decimal places
Output:
Value1 = 6.97
Value2 = 3.142
ROUND can be used to control the precision of calculations, especially when youâre dealing with scary big numbersâlike calculating how many fries youâve eaten this year. đ
Fun Example:
Scenario: Calculating your average quiz score but keeping it neat.
TotalScore â 86.374
Tests â 3
Average â ROUND(TotalScore DIV Tests, 2)
OUTPUT "Your average score is: ", Average
Python:
TotalScore = 86.374
Tests = 3
Average = round(TotalScore / Tests, 2)
print("Your average score is:", Average)
Output:
Your average score is: 28.79
"And no, you canât round your score to an A+!" đđ
RANDOM (Random Number Generation)â
RANDOM is the mischievous oneâit pulls a number out of thin air, keeping things exciting!
Pseudo-Code Example:
Value â RANDOM() // Generate a random number between 0 and 1
Python Translation:
import random
Value = random.random() # Generate a random number between 0 and 1
Output:
Value = 0.47853
Fun Example:
Scenario: Rolling a dice to see who pays the pizza bill.
DiceRoll â ROUND(RANDOM() * 6 + 1, 0)
OUTPUT "You rolled: ", DiceRoll
Python:
import random
DiceRoll = round(random.random() * 6 + 1, 0)
print("You rolled:", DiceRoll)
Output:
You rolled: 4
"Whoever rolls the lowest paysâno cheating!" đČđ
Library routines are like your ultimate sidekicks. They make your code cleaner, your life easier, and sometimes, they even save you from embarrassing math mistakes. So, go aheadâuse them wisely and with a touch of sass! âš