Codes in Python Language
FLAMES
def diff_letters(boy, girl):
boy = boy.lower()
girl = girl.lower()
boy_girl = len(boy) + len(girl)
same_letters = 0
for letter in boy:
for letter_2 in girl:
if letter == letter_2:
same_letters += 1
break;
for letter_2 in girl:
for letter in boy:
if letter_2 == letter:
same_letters += 1
break;
return boy_girl - int(same_letters)
def FLAMES(boy,girl):
if (diff_letters(boy,girl) % 6 == 1):
return "Friends"
elif (diff_letters(boy,girl) % 6 == 2):
return "Love"
elif (diff_letters(boy,girl) % 6 == 3):
return "Admiration"
elif (diff_letters(boy,girl) % 6 == 4):
return "Marriage"
elif (diff_letters(boy,girl) % 6 == 5):
return "Enemies"
else:
return "Soulmates"
def diff_letters(boy, girl):
boy = boy.lower()
girl = girl.lower()
boy_girl = len(boy) + len(girl)
same_letters = 0
for letter in boy:
for letter_2 in girl:
if letter == letter_2:
same_letters += 1
break;
for letter_2 in girl:
for letter in boy:
if letter_2 == letter:
same_letters += 1
break;
return boy_girl - int(same_letters)
def FLAMES(boy,girl):
if (diff_letters(boy,girl) % 6 == 1):
return "Friends"
elif (diff_letters(boy,girl) % 6 == 2):
return "Love"
elif (diff_letters(boy,girl) % 6 == 3):
return "Admiration"
elif (diff_letters(boy,girl) % 6 == 4):
return "Marriage"
elif (diff_letters(boy,girl) % 6 == 5):
return "Enemies"
else:
return "Soulmates"
REVERSE OF A LIST (no return function)
def reverse(nums):
""" (list) -> none
creates a function to reverse a list of nothing without returning a new list
"""
start = 0
for i in range(1, (len(nums) // 2) + 1):
prev= nums[start]
nums[start] = nums[-1]
nums[-i] = prev
start += 1
IDENTICAL ELEMENTS GIVEN 2 STRINGS
def identical(list_1, list_2):
count = 0
for i in range(0, len(list_1)):
for j in range(0, len(list_2)):
if (list_1[i] == list_2[j] and (i == j)):
count += 1
return count
def identical(list_1, list_2):
count = 0
for i in range(0, len(list_1)):
for j in range(0, len(list_2)):
if (list_1[i] == list_2[j] and (i == j)):
count += 1
return count
MAX OF 3 INTEGERS
def max_of_3(x, y, z):
"""(int, int, int)-> int
returns the largest value
"""
if ((x > y) and (x > z)):
return x
if ((y > z) and (y > x)):
return y
else:
return z
def max_of_3(x, y, z):
"""(int, int, int)-> int
returns the largest value
"""
if ((x > y) and (x > z)):
return x
if ((y > z) and (y > x)):
return y
else:
return z
DEGREE TO RADIANS
def radians(degrees):
"""(float) -> float
returns the radians value given the degrees
"""
return (math.pi * degrees) / 180
def radians(degrees):
"""(float) -> float
returns the radians value given the degrees
"""
return (math.pi * degrees) / 180
SHORTEST IN A LIST
def shortestName(list):
""" (list) -> str
Returns the shortest name in a given list
"""
sIndex = list[0]
for name in list:
if(len(sIndex) > len(name)):
sIndex = name
return sIndex
def shortestName(list):
""" (list) -> str
Returns the shortest name in a given list
"""
sIndex = list[0]
for name in list:
if(len(sIndex) > len(name)):
sIndex = name
return sIndex
WORD COUNT
def word_count(word):
""" (str) -> str
Returns the number of words given a string that may be separated by multiple spaces
"""
before = word[0]
ctr = 1
for character in word:
if(character == " " and not(before == " ")):
ctr = ctr + 1
before = character
return ctr
def word_count(word):
""" (str) -> str
Returns the number of words given a string that may be separated by multiple spaces
"""
before = word[0]
ctr = 1
for character in word:
if(character == " " and not(before == " ")):
ctr = ctr + 1
before = character
return ctr
LEAST COMMON MULTIPLE
def LCM(a, b):
""" (int, int) -> int
Design a function that determines the least common multiple of a given pair of
integers
"""
lcm = 0
for x in range(max(a, b), a * b + 1):
if(x % a == 0 and x % b == 0):
lcm = x
break
return lcm
def LCM(a, b):
""" (int, int) -> int
Design a function that determines the least common multiple of a given pair of
integers
"""
lcm = 0
for x in range(max(a, b), a * b + 1):
if(x % a == 0 and x % b == 0):
lcm = x
break
return lcm
FIRST N CUBES
def list_of_cubes(x):
""" (int) -> list of int
Design a function that creates a list of the first n cubes
assuming 1 is the first cube
"""
cubes = []
for n in range(1, x + 1):
cubes += [n ** 3]
return cubes
def list_of_cubes(x):
""" (int) -> list of int
Design a function that creates a list of the first n cubes
assuming 1 is the first cube
"""
cubes = []
for n in range(1, x + 1):
cubes += [n ** 3]
return cubes
SYRACUSE
def syracuse(num):
seq = [num]
x = num
while(x > 1):
if (x % 2 == 0):
x = x // 2
else:
x = (3 * x) + 1
seq += [x]
return seq
def syracuse(num):
seq = [num]
x = num
while(x > 1):
if (x % 2 == 0):
x = x // 2
else:
x = (3 * x) + 1
seq += [x]
return seq
DECIMAL TO BINARY
def binary(x):
"""(int) -> str
returns a string representing the binary equivalent of a given decimal integer.
"""
binary = ""
while(x > 0):
y = x % 2
binary = str(y) + binary
x = x // 2
return binary
def binary(x):
"""(int) -> str
returns a string representing the binary equivalent of a given decimal integer.
"""
binary = ""
while(x > 0):
y = x % 2
binary = str(y) + binary
x = x // 2
return binary
Codes in C Language
// Design a function that determines if an array of floats is sorted from smallest to largest
// values.
int small_large(float num[]) {
for (int i = 0; num[i] != '\0'; i++) {
if (num[i + 1] > num[i]);
return 1;
}
return 0;
}
// Design a function that determines the least common multiple of a given pair of
// integers.
// int lcm(int x, int y, int max) {
// if (x > y){
// max = x;
// } else {
// max = y;
// }
// for(; max <= (x * y); max++) {
// if (max % x == 0 && max % y == 0)
// break;
// }
// return max;
// }
int lcm(int x, int y){
int factor = (x * y);
for (int i = 1; i <= factor; i++);
if (i % x == 0 && i % y == 0){
return (i);
}
return 0;
}
// Design a function equals_ignore_case, which receives two char arrays and their lengths,
// and returns true if the two char arrays contain the same characters irrespective of
// the case. For example, for character arrays {'a', 'B', 'c'} and {'A', 'b', 'c'}, the function
// returns true, but for {'a', 'B', 'c'} and {'a', B'}, or {'a', 'B', 'c'} and {'X', 'b', 'z'}, the function
// returns false.
char to_upper(char letter) {
if ('a' <= letter && letter <= 'z'){
return letter - 32;
} else {
return letter;
}
}
int equals_ignore_case(char array1[], char array2[], int size1, int size2){
if (size1 != size2){
return 0;
} else {
for (int i = 0; i < size1; i++){
char ch1 = (to_upper(array1[i]));
char ch2 = (to_upper(array2[i]));
if (ch1 != ch2){
return 0;
} else {
return 1;
}
}
}
}
// Design a function that approximates the value of π by summing the terms of this
// series: 4/1 − 4/3 + 4/5 − 4/7 + 4/9 − 4/11 + ... The function accepts n (the number of
// terms to sum) and then returns the sum of the first n terms of this series.
float approx_pi(int terms) {
float dividend = 4;
float divisor = 1;
float sum = 0;
for(int i = 0; i < terms; i++) {
sum = sum + (dividend / divisor);
dividend *= -1;
divisor += 2;
}
return sum;
}
// str_length - accepts a string and returns the number of characters in the string
// str_length(“Hello World!”); // expect 12
int str_length(char phrase[]) {
int length = 0;
for(int i = 0; phrase[i] != '\0'; i++) {
length++;
}
return length;
}
// str_copy - accepts a source string and copies it into a destination string (assumes the
// destination array is long enough)
// char dest[20];
// str_copy("Hello world!", dest);
// printf("%s\n", dest); // prints “Hello world!”
void str_copy(char start[], char dest[]) {
for(int i = 0; start[i] != '\0'; i++) {
dest[i] = start[i];
}
}
// str_in - accepts two strings and checks if the second string is a substring of the first.
// str_in(“Hello World!”, “World”); // expect 1 (true)
// str_in(“Hello World!”, “ello”); // expect 1 (true)
// str_in(“Hello World!”, “hell”); // expect 0 (false)
int str_in(char string[], char substring[]) {
for(int i = 0; string[i] != '\0'; i++) {
int k = 0;
for(int j = i; string[j] != '\0'; j++) {
if(string[j] != substring[k]) {
break;
}
k++;
if(substring[k] == '\0') {
return 1;
}
}
}
return 0;
}
// Design a function that accepts an integer array, its length, and two integers "value" and
// "index". The function should put "value" in the "index" position of the array, shifting
// each element right and dropping off the last element.
void insert_drop_last(int arr_numbers[], int length, int value, int index) {
int prev = value;
int curr;
for(int i = index; i < length; i++) {
curr = arr_numbers[i];
arr_numbers[i] = prev;
prev = curr;
}
}
int main(){
float num_1[] = {1,2,3,4};
float num_2[] = {2,3,1,4};
float num_3[] = {1,2,3,1};
printf("%f\n", small_large(num_1));
printf("%f\n", small_large(num_2));
printf("%f\n", small_large(num_3));
printf("%f\n", lcm(2, 3));
printf("%f\n", lcm(10, 5));
printf("%f\n", lcm(4, 5));
char one[] = {'a', 'B', 'c'};
char two[] = {'a', 'b', 'c'};
char three[] = {'a', 'd', 'c'};
char four[] = {'c', 'b', 'a'};
printf("%d\n", equals_ignore_case(one, two, 3, 3));
printf("%d\n", equals_ignore_case(one, three, 3, 3));
printf("%d\n", equals_ignore_case(two, four, 3, 3));
printf("%d\n", equals_ignore_case(two, three, 3, 3));
printf("%f\n", approx_pi(3));
printf("%f\n", approx_pi(5));
printf("%f\n", approx_pi(7));
int arr_numbers[6] = {1, 2, 3, 4, 5, 6};
insert_drop_last(arr_numbers, 6, 100, 1);
for(int i = 0; i < 6; i++) {
printf("%d ", arr_numbers[i]);
char phrase[] = "Hello World!";
char phrase2[] = "Good Morning";
printf("%d\n", str_length(phrase));
printf("%d\n", str_length(phrase2));
char dest[20];
str_copy("Hello", dest);
printf("%s\n", dest);
printf("%d\n", str_in("Hello World!", "ello"));
printf("%d\n", str_in("Hello World!", "Hello"));
printf("%d\n", str_in("Hello World!", "World"));
return 0;
}
// values.
int small_large(float num[]) {
for (int i = 0; num[i] != '\0'; i++) {
if (num[i + 1] > num[i]);
return 1;
}
return 0;
}
// Design a function that determines the least common multiple of a given pair of
// integers.
// int lcm(int x, int y, int max) {
// if (x > y){
// max = x;
// } else {
// max = y;
// }
// for(; max <= (x * y); max++) {
// if (max % x == 0 && max % y == 0)
// break;
// }
// return max;
// }
int lcm(int x, int y){
int factor = (x * y);
for (int i = 1; i <= factor; i++);
if (i % x == 0 && i % y == 0){
return (i);
}
return 0;
}
// Design a function equals_ignore_case, which receives two char arrays and their lengths,
// and returns true if the two char arrays contain the same characters irrespective of
// the case. For example, for character arrays {'a', 'B', 'c'} and {'A', 'b', 'c'}, the function
// returns true, but for {'a', 'B', 'c'} and {'a', B'}, or {'a', 'B', 'c'} and {'X', 'b', 'z'}, the function
// returns false.
char to_upper(char letter) {
if ('a' <= letter && letter <= 'z'){
return letter - 32;
} else {
return letter;
}
}
int equals_ignore_case(char array1[], char array2[], int size1, int size2){
if (size1 != size2){
return 0;
} else {
for (int i = 0; i < size1; i++){
char ch1 = (to_upper(array1[i]));
char ch2 = (to_upper(array2[i]));
if (ch1 != ch2){
return 0;
} else {
return 1;
}
}
}
}
// Design a function that approximates the value of π by summing the terms of this
// series: 4/1 − 4/3 + 4/5 − 4/7 + 4/9 − 4/11 + ... The function accepts n (the number of
// terms to sum) and then returns the sum of the first n terms of this series.
float approx_pi(int terms) {
float dividend = 4;
float divisor = 1;
float sum = 0;
for(int i = 0; i < terms; i++) {
sum = sum + (dividend / divisor);
dividend *= -1;
divisor += 2;
}
return sum;
}
// str_length - accepts a string and returns the number of characters in the string
// str_length(“Hello World!”); // expect 12
int str_length(char phrase[]) {
int length = 0;
for(int i = 0; phrase[i] != '\0'; i++) {
length++;
}
return length;
}
// str_copy - accepts a source string and copies it into a destination string (assumes the
// destination array is long enough)
// char dest[20];
// str_copy("Hello world!", dest);
// printf("%s\n", dest); // prints “Hello world!”
void str_copy(char start[], char dest[]) {
for(int i = 0; start[i] != '\0'; i++) {
dest[i] = start[i];
}
}
// str_in - accepts two strings and checks if the second string is a substring of the first.
// str_in(“Hello World!”, “World”); // expect 1 (true)
// str_in(“Hello World!”, “ello”); // expect 1 (true)
// str_in(“Hello World!”, “hell”); // expect 0 (false)
int str_in(char string[], char substring[]) {
for(int i = 0; string[i] != '\0'; i++) {
int k = 0;
for(int j = i; string[j] != '\0'; j++) {
if(string[j] != substring[k]) {
break;
}
k++;
if(substring[k] == '\0') {
return 1;
}
}
}
return 0;
}
// Design a function that accepts an integer array, its length, and two integers "value" and
// "index". The function should put "value" in the "index" position of the array, shifting
// each element right and dropping off the last element.
void insert_drop_last(int arr_numbers[], int length, int value, int index) {
int prev = value;
int curr;
for(int i = index; i < length; i++) {
curr = arr_numbers[i];
arr_numbers[i] = prev;
prev = curr;
}
}
int main(){
float num_1[] = {1,2,3,4};
float num_2[] = {2,3,1,4};
float num_3[] = {1,2,3,1};
printf("%f\n", small_large(num_1));
printf("%f\n", small_large(num_2));
printf("%f\n", small_large(num_3));
printf("%f\n", lcm(2, 3));
printf("%f\n", lcm(10, 5));
printf("%f\n", lcm(4, 5));
char one[] = {'a', 'B', 'c'};
char two[] = {'a', 'b', 'c'};
char three[] = {'a', 'd', 'c'};
char four[] = {'c', 'b', 'a'};
printf("%d\n", equals_ignore_case(one, two, 3, 3));
printf("%d\n", equals_ignore_case(one, three, 3, 3));
printf("%d\n", equals_ignore_case(two, four, 3, 3));
printf("%d\n", equals_ignore_case(two, three, 3, 3));
printf("%f\n", approx_pi(3));
printf("%f\n", approx_pi(5));
printf("%f\n", approx_pi(7));
int arr_numbers[6] = {1, 2, 3, 4, 5, 6};
insert_drop_last(arr_numbers, 6, 100, 1);
for(int i = 0; i < 6; i++) {
printf("%d ", arr_numbers[i]);
char phrase[] = "Hello World!";
char phrase2[] = "Good Morning";
printf("%d\n", str_length(phrase));
printf("%d\n", str_length(phrase2));
char dest[20];
str_copy("Hello", dest);
printf("%s\n", dest);
printf("%d\n", str_in("Hello World!", "ello"));
printf("%d\n", str_in("Hello World!", "Hello"));
printf("%d\n", str_in("Hello World!", "World"));
return 0;
}