Skip to content

Commit

Permalink
Added solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
xehpuk committed Mar 20, 2018
0 parents commit e54adb1
Show file tree
Hide file tree
Showing 514 changed files with 15,765 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 2-plus-2-star-2-problem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 2 + 2 * 2 problem
[*View on Codewars*](https://www.codewars.com/kata/2-plus-2-star-2-problem)

Steve loves MS Windows calculator in Standard mode.

He tries to calculate ``2 + 2 * 2`` in his favorite programming language. But Steve doesn't know about order of operations and he wonders why answers are different. He expects ``8`` but gets ``6``.

Help Steve to fix program, so result will be identical to MS Windows calculator.

## Timeline
- Created: 2014-05-18
- Published: 2014-05-18
- Approved: 2014-05-22
- Completed: 2015-05-05
6 changes: 6 additions & 0 deletions 2-plus-2-star-2-problem/java/1/TwoPlusTwoTimesTwoProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class TwoPlusTwoTimesTwoProblem {

public static int calculate() {
return 1 << 3;
}
}
6 changes: 6 additions & 0 deletions 2-plus-2-star-2-problem/java/2/TwoPlusTwoTimesTwoProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class TwoPlusTwoTimesTwoProblem {

public static int calculate() {
return 0b1000;
}
}
6 changes: 6 additions & 0 deletions 2-plus-2-star-2-problem/java/3/TwoPlusTwoTimesTwoProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class TwoPlusTwoTimesTwoProblem {

public static int calculate() {
return Integer.highestOneBit(new java.util.Random().nextInt(8) + 8);
}
}
59 changes: 59 additions & 0 deletions 254-shades-of-grey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 254 shades of grey
[*View on Codewars*](https://www.codewars.com/kata/254-shades-of-grey)

Why would we want to stop to only 50 shades of grey? Let's see to how many we can go.

Write a function that takes a number n as a parameter and return an array containing n shades of grey in hexadecimal code (`#aaaaaa` for example). The array should be sorted in ascending order starting with `#010101`, `#020202`, etc. (using lower case letters).

```javascript
function shadesOfGrey(n){
// returns n shades of grey in an array
}
```
```coffeescript
shadesOfGrey = (n) ->
# returns n shades of grey in an array
```
```haskell
shadesOfGrey :: Int -> [String]
```
```csharp
using System;

public static class Kata {
public static class ShadesOfGrey(int n) {
// returns n shades of grey in an array
}
}
```
```python
def shades_of_grey(n):
return '''n shades of grey in an array'''
```
```ruby
def shades\_of\_grey(n)
return '''n shades of grey in an array'''
end
```
```java
public class ShadesOfGrey {
static String[] shadesOfGrey(int num) {
// returns n shades of grey in an array
}
}
```

As a reminder, the grey color is composed by the same number of red, green and blue: `#010101`, `#aeaeae`, `#555555`, etc. Also, `#000000` and `#ffffff` are not accepted values.

When n is negative, just return an empty array.
If n is higher than 254, just return an array of 254 elements.

Have fun



## Timeline
- Created: 2015-02-04
- Published: 2015-02-04
- Approved: 2015-03-10
- Completed: 2015-05-24
20 changes: 20 additions & 0 deletions 254-shades-of-grey/csharp/1/254-shades-of-grey
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

public static class Kata
{
public static string[] ShadesOfGrey(int n)
{
if (n <= 0)
{
return new string[0];
}
var max = Math.Min(byte.MaxValue - 1, n);
var shadesOfGrey = new string[max];
for (int i = 0; i < max; i++)
{
var hex = (i + 1).ToString("x2");
shadesOfGrey[i] = '#' + hex + hex + hex;
}
return shadesOfGrey;
}
}
14 changes: 14 additions & 0 deletions 254-shades-of-grey/csharp/2/254-shades-of-grey
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Linq;

public static class Kata
{
public static string[] ShadesOfGrey(int n)
{
return Enumerable
.Range(1, byte.MaxValue - 1)
.Take(n)
.Select(i => i.ToString("x2"))
.Select(x => '#' + x + x + x)
.ToArray();
}
}
11 changes: 11 additions & 0 deletions 254-shades-of-grey/java/1/ShadesOfGrey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ShadesOfGrey {
static String[] shadesOfGrey(int n) {
return n <= 0 ? new String[0]
: java.util.stream.IntStream
.range(1, 0xFF)
.limit(n)
.mapToObj(i -> String.format("%02x", i))
.map(x -> '#' + x + x + x)
.toArray(String[]::new);
}
}
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) , All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.
38 changes: 38 additions & 0 deletions a-function-to-display-mottos-for-westerosi-houses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# A function to display Mottos for Westerosi Houses
[*View on Codewars*](https://www.codewars.com/kata/a-function-to-display-mottos-for-westerosi-houses)

Given a list of the following major Houses of Westeros and their respective mottos:

```
var houses = [
{name: "Targaryen", motto: "Fire and Blood"},
{name: "Stark", motto: "Winter is Coming"},
{name: "Bolton", motto: "Our Blades Are Sharp"},
{name: "Greyjoy", motto: "We Do Not Sow"},
{name: "Tully", motto: "Family, Duty, Honor"},
{name: "Arryn", motto: "As High as Honor"},
{name: "Lannister", motto: "Hear Me Roar!"},
{name: "Tyrell", motto: "Growing Strong"},
{name: "Baratheon", motto: "Ours is the Fury"},
{name: "Martell", motto: "Unbowed, Unbent, Unbroken"}
];```
Write a function that, when passed the name of a House, returns its motto. For instance:
```
motto("Tyrell")
```
should return
```
"growing strong"
```
If passed an invalid House name, the script should return an empty string.
## Timeline
- Created: 2014-06-04
- Published: 2014-06-04
- Approved: null
- Completed: 2015-05-23
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var houses = [
{name: "Targaryen", motto: "Fire and Blood"},
{name: "Stark", motto: "Winter is Coming"},
{name: "Bolton", motto: "Our Blades Are Sharp"},
{name: "Greyjoy", motto: "We Do Not Sow"},
{name: "Tully", motto: "Family, Duty, Honor"},
{name: "Arryn", motto: "As High as Honor"},
{name: "Lannister", motto: "Hear Me Roar!"},
{name: "Tyrell", motto: "Growing Strong"},
{name: "Baratheon", motto: "Ours is the Fury"},
{name: "Martell", motto: "Unbowed, Unbent, Unbroken"}
];

function motto(name) {
for (var i = 0; i < houses.length; i++) {
var house = houses[i];
if (house.name === name) {
return house.motto;
}
}
return "";
}
51 changes: 51 additions & 0 deletions a-needle-in-the-haystack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# A Needle in the Haystack
[*View on Codewars*](https://www.codewars.com/kata/a-needle-in-the-haystack)

Can you find the needle in the haystack?

Write a function `findNeedle()` that takes an `array` full of junk but containing one `"needle"`

After your function finds the needle it should return a message (as a string) that says:

`"found the needle at position "` plus the `index` it found the needle

So

````python
find_needle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'])
````
````ruby
find_needle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'])
````
````javascript
findNeedle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'])
````
````java
findNeedle(new Object[] {"hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"})
````
````elixir
find_needle(["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"])
````

should return
````python
'found the needle at position 5'
````
````ruby
'found the needle at position 5'
````
````javascript
'found the needle at position 5'
````
````java
"found the needle at position 5"
````
````elixir
"found the needle at position 5"
````

## Timeline
- Created: 2015-12-08
- Published: 2015-12-09
- Approved: 2015-12-28
- Completed: 2016-09-08
9 changes: 9 additions & 0 deletions a-needle-in-the-haystack/java/1/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Kata {
public static String findNeedle(Object[] haystack) {
for (int i = 0;; i++) {
if ("needle".equals(haystack[i])) {
return "found the needle at position " + i;
}
}
}
}
10 changes: 10 additions & 0 deletions a-plus-b/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# A + B
[*View on Codewars*](https://www.codewars.com/kata/a-plus-b)

Vasya Pupkin just started learning Java and C#. At first, he decided to write simple program that was supposed to sum up two small numbers (numbers and their sum fit in a byte), but it didn't work. Vasya was too sad to find out what is wrong. Help him to correct the mistake.

## Timeline
- Created: 2015-03-25
- Published: 2015-03-25
- Approved: 2016-06-29
- Completed: 2015-05-16
5 changes: 5 additions & 0 deletions a-plus-b/java/1/FirstClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class FirstClass {
public static int sum (int a, int b) {
return a+b;
}
}
11 changes: 11 additions & 0 deletions addition-function-problem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# addition function problem
[*View on Codewars*](https://www.codewars.com/kata/addition-function-problem)

The function is incomplete and it's your job to complete it
so it would add the given values.

## Timeline
- Created: 2014-02-08
- Published: 2014-02-08
- Approved: null
- Completed: 2015-05-21
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function add(a, b){
return a + b
}
12 changes: 12 additions & 0 deletions are-the-brackets-balanced/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Are the brackets balanced?
[*View on Codewars*](https://www.codewars.com/kata/are-the-brackets-balanced)

Given a string which may include opening or closing round brackets, can you tell whether the string contains a balanced pair(s) of round brackets, that is there are no brackets which are either opened & not closed, or vice versa. Opening round brackets always have to come before closing bracket. An empty string is balanced.



## Timeline
- Created: 2015-05-09
- Published: 2015-05-09
- Approved: null
- Completed: 2015-05-20
29 changes: 29 additions & 0 deletions are-the-brackets-balanced/java/1/BrakedBalance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Deque;
import java.util.concurrent.LinkedBlockingDeque;

public class BrakedBalance {
private static final char CLOSING = ')';
private static final char OPENING = '(';

public static boolean isStringBalanced(final String s) {
final int length = s.length();
final Deque<Character> openings = new LinkedBlockingDeque<>(length / 2);
for (int i = 0; i < length; i++) {
if (!isCharBalanced(openings, s.charAt(i))) {
return false;
}
}
return openings.isEmpty();
}

private static boolean isCharBalanced(final Deque<Character> openings, final char c) {
if (c == OPENING) {
return openings.offerFirst(c);
}
if (c == CLOSING) {
final Character bracket = openings.pollFirst();
return bracket != null && bracket == OPENING;
}
return false;
}
}
Loading

0 comments on commit e54adb1

Please sign in to comment.