Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Last active December 23, 2015 08:23
Show Gist options
  • Save jjlumagbas/0273a993bad290ca50f5 to your computer and use it in GitHub Desktop.
Save jjlumagbas/0273a993bad290ca50f5 to your computer and use it in GitHub Desktop.
My solution to CMSC11 Final last 4 questions
#include <stdio.h>
void display(int arr[], int length) {
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// 1
void rotate(int arr[], int length) {
int last = arr[length - 1];
for (int i = length - 2; i >= 0; i--) {
arr[i + 1] = arr[i];
}
arr[0] = last;
}
// 2
int length(char str[]) {
int i = 0;
while (str[i] != '\0') {
i++;
}
return i;
}
int sliceGoodness(char str[], int sliceLength, int startIndex) {
int goodness = 0;
for (int i = startIndex, count = 0; str[i] != '\0' && count < sliceLength; i++, count++) {
if (str[i] == 'u') {
return 0;
} else if (str[i] == 'g') {
goodness++;
}
}
return goodness;
}
int highestGoodness(char str[], int n) {
if (n == 0) {
return -1;
}
int highest = 0;
int highestIndex = 0;
int strLength = length(str);
for (int i = 0; i + n <= strLength; i++) {
int goodness = sliceGoodness(str, n, i);
if (goodness > highest) {
highest = goodness;
highestIndex = i;
}
}
return highestIndex;
}
// 3
int convertToNumber(char num) {
if ('0' <= num && num <= '9') {
return num - '0';
} else if ('A' <= num && num <= 'C') {
return 2;
} else if ('D' <= num && num <= 'F') {
return 3;
} else if ('G' <= num && num <= 'I') {
return 4;
} else if ('J' <= num && num <= 'L') {
return 5;
} else if ('M' <= num && num <= 'O') {
return 6;
} else if ('P' <= num && num <= 'S') {
return 7;
} else if ('T' <= num && num <= 'V') {
return 8;
} else if ('W' <= num && num <= 'Z') {
return 9;
}
return 0;
}
int numericPhone(char num[]) {
int numeric = 0;
for (int i = 0; num[i] != '\0'; i++) {
numeric *= 10;
numeric += convertToNumber(num[i]);
}
return numeric;
}
// 4
int isPrime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int isHumble(int n) {
for (int i = 2; i <= n; i++) {
if (n % i == 0 && i != 2 && i != 3 && i != 5 && i != 7 && isPrime(i)) {
return 0;
}
}
return 1;
}
int main() {
printf("%d = 1", isPrime(13));
for (int i = 0; i < 100; i++) {
if (isHumble(i)) {
printf("%d ", i);
}
}
printf("\n");
printf("%d\n", numericPhone("243NERD"));
printf("%d\n", length("abcde"));
printf("%d = 0\n", highestGoodness("ggg", 3));
printf("%d = 1\n", highestGoodness("bggg", 3));
printf("%d = 2\n", highestGoodness("bbgggu", 3));
printf("%d = 0\n", highestGoodness("ggg", 3));
printf("%d = 3\n", sliceGoodness("ggg", 3, 0));
printf("%d = 0\n", sliceGoodness("bbb", 3, 0));
printf("%d = 0\n", sliceGoodness("gug", 3, 0));
printf("%d = 1\n", sliceGoodness("gbb", 3, 0));
printf("%d = 1\n", sliceGoodness("gbb", 2, 0));
printf("%d = 0\n", sliceGoodness("bbg", 2, 0));
printf("%d = 0\n", highestGoodness("ggg", 3));
int nums[5] = {1, 2, 3, 4, 5};
display(nums, 5); // 1 2 3 4 5
rotate(nums, 5);
display(nums, 5); // 5 1 2 3 4
int num[1] = {1};
display(num, 1); // 1 2 3 4 5
rotate(num, 1);
display(num, 1); // 5 1 2 3 4
return 0;
}
@jjlumagbas
Copy link
Author

  1. Create a function that "rotates" an integer array clockwise, given an array and its length. It moves all values one position to the right, with the rightmost element moving to the leftmost position. For example:

    int nums[5] = {1, 2, 3, 4, 5};
    
    display(nums, 5); // 1 2 3 4 5
    rotate(nums, 5);
    display(nums, 5); // 5 1 2 3 4
    

@jjlumagbas
Copy link
Author

  1. Consider strings where each character is one of the following three characters:
  • g for “good”
  • b for “bad”
  • u for “unusable”

The goodness of a string follows these two rules:

  • The goodness of a string containing one or more u’s is 0.
  • Otherwise, the goodness of a string is equal to the number of g’s in the string.

For example, the goodness of "gbbgb" is 2, and the goodness of "gubgb" is 0. Remember that a slice of a string is a string, and so the goodness of a slice is defined by these same rules. Any string has more than one slice you can take from it (unless it is a very short string). Each slice may have a different goodness value.

Create a function that accepts a string str and a slice length n. The function then returns the starting index of the slice with length n having the highest goodness value in that string. If n is zero, return -1.

@jjlumagbas
Copy link
Author

  1. Create a function that accepts an alphanumeric phone number (a string), and returns the equivalent numeric phone number (an integer). Don’t print in the function! Instead, return an integer.

For example, given "243NERD", return 2346373.

Here are the International Standard keypad mappings:

2 - ABC
3 - DEF
4 - GHI
5 - JKL
6 - MNO
7 - PQRS
8 - TUV
9 - WXYZ

@jjlumagbas
Copy link
Author

  1. Create a function that accepts a number and determines whether it is a humble number.

A positive whole number n > 2 is prime if no number between 2 and n / 2 (inclusive) evenly divides n. A prime factor is a factor of a number that is a prime number.

A number whose only prime factors are 2, 3, 5, or 7 is called a humble number. The sequence 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27 shows the first 19 humble numbers. 39 is not humble, because while it has 3 as a prime factor, it also has 13 as another prime factor, which is neither 2, 3, 5, nor 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment