[CodeWars] Mumbling

This time no story, no theory. The examples below show you how to write function accum:

Examples:

Accumul::accum("abcd");    // "A-Bb-Ccc-Dddd"
Accumul::accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
Accumul::accum("cwAt");    // "C-Ww-Aaa-Tttt"

The parameter of accum is a string which includes only letters from a..z and A..Z.

More

[CodeWars] Bubblesort Once

Bubblesort Algorithm

Overview

The Bubblesort Algorithm is one of many algorithms used to sort a list of similar items (e.g. all numbers or all letters) into either ascending order or descending order. Given a list (e.g.):

{9, 7, 5, 3, 1, 2, 4, 6, 8}

To sort this list in ascending order using Bubblesort, you first have to compare the first two terms of the list. If the first term is larger than the second term, you perform a swap. The list then becomes:

{7, 9, 5, 3, 1, 2, 4, 6, 8} // The "9" and "7" have been swapped because 9 is larger than 7 and thus 9 should be after 7

You then proceed by comparing the 2nd and 3rd terms, performing a swap when necessary, and then the 3rd and 4th term, then the 4th and 5th term, etc. etc. When you reach the end of the list, it is said that you have completed 1 complete pass.

More

[CodeWars] Find odd int

Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number of times.


My solution

#include <vector>

int findOdd(const std::vector<int>& numbers) {
    for (int it : numbers)
    {
        if ((std::count(numbers.begin(), numbers.end(), it) % 2> 0)
            return it;
    }
}
cs

Use the std:count() function to count each number. If the number of counted is an odd number, it is printed. And if it is an even number, it just passed.

[CodeWars] Is a number prime?

Define a function isPrime/is_prime() that takes one integer argument and returns true/True or false/False depending on if the integer is a prime.

Per Wikipedia, a prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Example

bool isPrime(5= return true
cs

Assumptions

  • You can assume you will be given an integer input.
  • You can not assume that the integer will be only positive. You may be given negative numbers as well (or 0).


More

[CodeWars] Roman Numerals Encoder

Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.

Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.

More