How to Check for All Inclusive in C

The challenge Input: a string strng an array of strings arr Output of function contain_all_rots(strng, arr) (or containAllRots or contain-all-rots): a boolean true if all rotations of strng are included in arr false otherwise Examples: contain_all_rots( "bsjq", ["bsjq", "qbsj", "sjqb", "twZNsslC", "jqbs"]) -> true contain_all_rots( "Ajylvpy", ["Ajylvpy", "ylvpyAj", "jylvpyA", "lvpyAjy", "pyAjylv", "vpyAjyl", "ipywee"]) -> false) Note: Though not correct in a mathematical sense we will consider that there are no rotations of strng == "" and for any array arr: contain_all_rots("", arr) --> true The solution in C Option 1:...

November 27, 2022 · 3 min · 448 words · Andrew

How to Solve for Factorial in C

The challenge In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1. Write a function to calculate factorial for a given input. If input is below 0 or above 12 return -1 (C)....

November 26, 2022 · 1 min · 205 words · Andrew

How to Create an Incrementer in C

The challenge Given an input of an array of digits, return the array with each digit incremented by its position in the array: the first digit will be incremented by 1, the second digit by 2, etc. Make sure to start counting your positions from 1 ( and not 0 ). Your result can only contain single digit numbers, so if adding a digit with its position gives you a multiple-digit number, only the last digit of the number should be returned....

November 25, 2022 · 3 min · 613 words · Andrew

How to Return the Closest Number Multiple of 10 in C

The challenge Given a number return the closest number to it that is divisible by 10. Example input: 22 25 37 Expected output: 20 30 40 The solution in C Option 1: #include <math.h> int round_to_10 (int n) { return round(n / 10.0) * 10; } Option 2: int round_to_10(int n) { return (n + 5) / 10 * 10; } Option 3: int round_to_10 (int n) { int r = n % 10; if (r > 0) return r < 5 ?...

November 24, 2022 · 1 min · 176 words · Andrew

How to Reverse Every Other Word in a String in C

The challenge Reverse every other word in a given string, then return the string. Throw away any leading or trailing whitespace, while ensuring there is exactly one space between each word. Punctuation marks should be treated as if they are a part of the word in this challenge. The solution in C Option 1: #include <stddef.h> #include <stdbool.h> void reverse_alternate(const char *string, char *result) { bool is_word = false, is_second = false, not_first = false; for (const char *s = string; *s; ++s) { if (*s == ' ' && is_word) { is_word = false; is_second = !...

November 23, 2022 · 3 min · 533 words · Andrew

How to Solve Simple Beads Count in C

The challenge Two red beads are placed between every two blue beads. There are N blue beads. After looking at the arrangement below work out the number of red beads. @ @@ @ @@ @ @@ @ @@ @ @@ @ Implement count_red_beads(n) (countRedBeads(n)) so that it returns the number of red beads. If there are less than 2 blue beads return 0. The solution in C Option 1: int countRedBeads(n) { if(n<=1) return 0; return 2*(n-1); } Option 2:...

November 22, 2022 · 1 min · 135 words · Andrew

How to Solve the Maze Runner in C

The challenge Introduction Welcome Adventurer. Your aim is to navigate the maze and reach the finish point without touching any walls. Doing so will kill you instantly! Task You will be given a 2D array of the maze and an array of directions. Your task is to follow the directions given. If you reach the end point before all your moves have gone, you should return Finish. If you hit any walls or go outside the maze border, you should return Dead....

November 21, 2022 · 4 min · 821 words · Andrew

How to Take a Number and Sum It’s Digits Raied to the Consecutive Powers in C

The challenge The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this challenge. What’s the use of saying “Eureka”? Because this sum gives the same number. In effect: 89 = 8^1 + 9^2 The next number in having this property is 135. See this property again: 135 = 1^1 + 3^2 + 5^3 We need a function to collect these numbers, that may receive two integers a, b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above....

November 20, 2022 · 4 min · 779 words · Andrew

How to Find the Middle Element in C

The challenge You need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements. The input to the function will be an array of three distinct numbers (Haskell: a tuple). For example: gimme([2, 3, 1]) => 0 2 is the number that fits between 1 and 3 and the index of 2 in the input array is __....

November 19, 2022 · 2 min · 276 words · Andrew

How to Invite More Women in C

The challenge Task King Arthur and his knights are having a New Years party. Last year Lancelot was jealous of Arthur, because Arthur had a date and Lancelot did not, and they started a duel. To prevent this from happening again, Arthur wants to make sure that there are at least as many women as men at this year’s party. He gave you a list of integers of all the party goers....

November 18, 2022 · 2 min · 288 words · Andrew