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: 1 2 3 22 25 37 Expected output: 1 2 3 20 30 40 The solution in C Option 1: 1 2 3 4 5 #include <math.h> int round_to_10 (int n) { return round(n / 10.0) * 10; } Option 2: 1 2 3 int round_to_10(int n) { return (n + 5) / 10 * 10; } Option 3:

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <stddef.

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: 1 2 3 4 int countRedBeads(n) { if(n<=1) return 0; return 2*(n-1); } Option 2:

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.

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.

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: 1 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 __.

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.

How to Build a Tower in C

The challenge Build a pyramid-shaped tower, as an array/list of strings, given a positive integer number of floors. A tower block is represented with "*" character. For example, a tower with 3 floors looks like this: 1 2 3 4 5 [ " * ", " *** ", "*****" ] And a tower with 6 floors looks like this: 1 2 3 4 5 6 7 8 [ " * ", " *** ", " ***** ", " ******* ", " ********* ", "***********" ] The solution in C Option 1:

How to Calculate A Rule of Divisibility by 7 in C

The challenge A number m of the form 10x + y is divisible by 7 if and only if x − 2y is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a number known to be divisible by 7 is obtained; you can stop when this number has at most 2 digits because you are supposed to know if a number of at most 2 digits is divisible by 7 or not.

How to Categorize a New Member in C

The challenge The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed. To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.

How to Solve Deodorant Evaporator in C

The challenge This program tests the life of an evaporator containing a gas. We know the content of the evaporator (content in ml), the percentage of foam or gas lost every day (evap_per_day) and the threshold (threshold) in percentage beyond which the evaporator is no longer useful. All numbers are strictly positive. The program reports the nth day (as an integer) on which the evaporator will be out of use.

How to Find the Divisors in C

The challenge Create a function named divisors/Divisors that takes an integer n > 1 and returns an array with all of the integer’s divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string ‘(integer) is prime’ (null in C#) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust). Example: 1 2 3 divisors(12); // results in {2, 3, 4, 6} divisors(25); // results in {5} divisors(13); // results in NULL The solution in C Option 1:

Solving Love vs Friendship in C

The challenge If a = 1, b = 2, c = 3 ... z = 26 Then l + o + v + e = 54 and f + r + i + e + n + d + s + h + i + p = 108 So friendship is twice as strong as love 🙂 Your task is to write a function which calculates the value of a word based off the sum of the alphabet positions of its characters.

How to Bounce Balls in C

The challenge A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known. He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66). His mother looks out of a window 1.5 meters from the ground. How many times will the mother see the ball pass in front of her window (including when it’s falling and bouncing?

How to Find the Capitals in C

The challenge Instructions Write a function that takes a single string (word) as argument. The function must return an ordered list containing the indexes of all capital letters in the string. Example 1 Test.assertSimilar( capitals('CodEStAr'), [0,3,4,6] ); The solution in C Option 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stddef.h> #include <string.h> #include <stdlib.h> size_t *find_capitals(const char *word, size_t *nb_uppercase) { size_t n = strlen(word); size_t *arr = (size_t *) calloc(n, sizeof(size_t)); size_t j = 0; for(size_t i=0; i<n; i++) { if(word[i] >='A' && word[i] <='Z') { arr[j++] = i; } } *nb_uppercase = j; return realloc(arr, j * sizeof(size_t)); } Option 2:

How to Empty and Delete an S3 Bucket using the AWS CLI

Option 1 – Using AWS CLI Step 1 1 export bucketname='your-bucket-here' Step 2 1 aws s3api delete-objects --bucket $bucketname --delete "$(aws s3api list-object-versions --bucket $bucketname --output=json --query='{Objects: *[].{Key:Key,VersionId:VersionId}}')"; Step 3 1 aws s3 rb s3://$bucketname Option 2 – Using Python 1 2 3 4 5 6 7 8 9 10 11 #!/usr/bin/env python BUCKET = 'your-bucket-here' import boto3 s3 = boto3.resource('s3') bucket = s3.Bucket(BUCKET) bucket.object_versions.delete() bucket.delete()

What ports to open for FSx connection to AWS Managed Active Directory

If you are creating a FSx file system, and want to connect it to AWS Managed Active Directory, then you will need to create a VPC Security Group with the following ports: Inbound ports Rules Ports UDP 53, 88, 123, 389, 464 TCP 53, 88, 123, 389, 445, 464, 636, 3268, 3269, 9389, 49152-65535 Outbound ports All traffic, 0.0.0.0/0

How to Calculate Variance in Python

If you need to calculate variance in Python, then you can do the following. Option 1 – Using variance() from Statistics module 1 2 3 4 5 6 7 import statistics list = [12,14,10,6,23,31] print("List : " + str(list)) var = statistics.variance(list) print("Variance: " + str(var)) Option 2 – Using var() from numpy module 1 2 3 4 5 6 import numpy as np arr = [12,43,24,17,32] print("Array : ", arr) print("Variance: ", np.

How to Calculate the Sum of a List in Python

If you need to calculate and get the sum of a list in Python, then you can do the following. Option 1 – Using sum() 1 2 3 myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] listSum = sum(myList) print(f"Sum of list -> {listSum}") If you get a TypeError then you can do the following: 1 2 3 4 5 6 myList = ["1", "3", "5", "7", "9"] myNewList = [int(string) for string in myList] sum1 = sum(myNewList) sum2 = sum(number for number in myNewList) print(f"Sum of list -> {sum1}") print(f"Sum of list -> {sum2}") Option 2 – Using for 1 2 3 4 5 6 7 8 myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] length = len(myList) listSum = 0 for i in range(length): listSum += myList[i] print(f"Sum of list -> {listSum}")

How to add a List to a Set in Python

If you need to add a list to a set in Python, then you can do the following: Option 1 – Using Tuple 1 2 3 4 5 myset = set((1,2,3,4)) mylist = list(1,2,3]) myset.add(tuple(mylist)) print(myset) Output: {1, 2, 3, 4, (1, 2, 3)} Option 2 – Using set.update() 1 2 3 4 5 myset = set((1,2,3,4)) mylist = list(8,9,12]) myset.update(tuple(mylist)) print(myset) Output: {1, 2, 3, 4, 8, 9, 12}