├── CSharp ├── 4KYU │ ├── Braces.cs │ └── ConnectFour.cs ├── 5KYU │ └── CamelCase.cs ├── 6KYU │ ├── BuildTower.cs │ ├── FindOdd.cs │ ├── GA-DE-RY-PO-LU-KI2.cs │ ├── MissingAlpha.cs │ ├── Multiples3and5.cs │ └── Smile.cs ├── 7KYU │ ├── Charitable.cs │ ├── Cypher1.cs │ ├── Cypher2.cs │ ├── GA-DE-RY-PO-LU-KI.cs │ ├── Histogram.cs │ ├── Lamps.cs │ ├── Sheep.cs │ ├── Sum0ToN.cs │ └── Tag.cs ├── 8KYU │ ├── ab.cs │ ├── bindec.cs │ ├── bus.cs │ ├── enumerable.cs │ ├── hoop.cs │ ├── invertvalues.cs │ ├── loop.cs │ ├── multiply.cs │ ├── rps.cs │ ├── squareroot.cs │ ├── subsum.cs │ ├── swap.cs │ ├── switch.cs │ ├── theatre.cs │ └── welcome.cs ├── Authored │ ├── CarPark.cs │ ├── DrivingLicense.cs │ ├── Drunk.cs │ ├── Fish.cs │ ├── Fruit.cs │ ├── Hippo.cs │ ├── Square.cs │ ├── connect4.cs │ ├── maze.cs │ ├── snakes.cs │ └── wave.cs ├── Beta │ ├── Decimal.cs │ └── MissingLetters.cs ├── CSharp.md └── Translation │ └── battleships.cs ├── Javascript ├── 5KYU │ ├── Bulk.js │ └── Flatten.js ├── 6KYU │ ├── 283.js │ ├── 305.js │ ├── Bee.js │ ├── Bingo.js │ ├── CountChar.js │ ├── Dollars.js │ ├── Duplicates.js │ ├── FizzBuzz.js │ ├── Guess.js │ ├── NumSearch.js │ ├── Range1.js │ ├── Range2.js │ ├── Replace.js │ ├── Select.js │ ├── SortOdd.js │ └── Unique.js ├── 7KYU │ ├── Args.js │ ├── Change.js │ ├── Column.js │ ├── Debug.js │ ├── Dict.js │ ├── DigitSum.js │ ├── Digits.js │ ├── DrivingSchool1.js │ ├── Even.js │ ├── Extend.js │ ├── Flatten.js │ ├── Gift.js │ ├── GreenBottle.js │ ├── Grey.js │ ├── Ifs.js │ ├── IsEven.js │ ├── Kooka.js │ ├── Merge.js │ ├── NumStarLadder.js │ ├── OneOne.js │ ├── OnlyOne.js │ ├── Order.js │ ├── RandomCase.js │ ├── RemoveDup.js │ ├── SortLen.js │ ├── SplitCase.js │ ├── Sum35.js │ ├── SumCube.js │ ├── SumOdd.js │ ├── Tiers.js │ ├── Value.js │ ├── Vests.js │ └── Words.js ├── 8KYU │ ├── 261.js │ ├── Broken.js │ ├── CalAvg.js │ ├── Clean.js │ ├── First.js │ ├── FizzBuzz1.js │ ├── FizzBuzz2.js │ ├── Fold.js │ ├── Guess.js │ ├── MaxHeadRoom.js │ └── Nice.js ├── Authored │ ├── CarPark.js │ ├── Connect4.js │ ├── DrivingLicense.js │ ├── Drunk.js │ ├── Fish.js │ ├── Fruit.js │ ├── GuessWho.js │ ├── Hippo.js │ ├── Mastermind.js │ ├── MazeRunner.js │ ├── SnakeLadders.js │ ├── Square.js │ ├── VendingMachine.js │ ├── Wave.js │ └── scooby.js ├── Beta │ ├── 262.js │ ├── 263.js │ ├── 264.js │ ├── 3Root.js │ ├── AddWord2.js │ ├── Alternate.js │ ├── Bowling.js │ ├── BowlingPins.js │ ├── Calc.js │ ├── Cancer.js │ ├── CountKid.js │ ├── CountWords.js │ ├── Cut.js │ ├── Dora.js │ ├── Drunk.js │ ├── Easy.js │ ├── Fruit.js │ ├── Hidden.js │ ├── Love.js │ ├── Manipulation.js │ ├── Merge.js │ ├── NextLine.js │ ├── NorthSouth.js │ ├── Password.js │ ├── RPG.js │ ├── ReplaceFirst.js │ ├── Shape.js │ ├── Stairs.js │ ├── WhoAmI.js │ ├── battle.js │ └── fish.js └── Javascript.md ├── PHP ├── 7KYU │ └── Jenny.php ├── 8KYU │ └── Fold.php └── PHP.md ├── Python ├── 3KYU │ └── Calc.py ├── 5KYU │ └── Bulk.py ├── 6KYU │ ├── Doors.py │ ├── FizzBuzz.py │ ├── GA-DE-RY-PO-LU-KI2.py │ ├── Order.py │ ├── Replace.py │ └── Treasure.py ├── 7KYU │ ├── Check.py │ ├── Flatten.py │ ├── GA-DE-RY-PO-LU-KI.py │ ├── GuessNum.py │ ├── ISEven.py │ ├── Ifs.py │ ├── Sheep.py │ ├── Spend.py │ └── Spot.py ├── 8KYU │ ├── FindNumber.py │ └── N.py ├── Authored │ ├── CarPark.py │ ├── Connect4.py │ ├── Drunk.py │ ├── Fish.py │ ├── Fruit.py │ ├── GuessWho.py │ ├── Hippo.py │ ├── Mastermind.py │ ├── MazeRunner.py │ ├── SnakeLadders.py │ ├── Square.py │ ├── VendingMachine.py │ ├── Wave.py │ └── scooby.py ├── Beta │ ├── 2Numbers.py │ ├── AddWord2.py │ ├── Bowling.py │ ├── Cookie.py │ ├── Div.py │ ├── Easy.py │ ├── Luck.py │ └── WhoAmI.py ├── Python.md └── Translation │ ├── BattleShips.py │ └── BowlingPins.py ├── README.md ├── Ruby ├── 3KYU │ ├── Calculator.rb │ ├── DidYouMean.rb │ └── HardBomb.rb ├── 4KYU │ ├── Brace.rb │ ├── FluentCalc.rb │ ├── FreqWord.rb │ ├── LifeOfPossibilities.rb │ ├── LongSub.rb │ ├── NextBigNum.rb │ ├── RomanNumerals.rb │ ├── StringMix.rb │ ├── StripComments.rb │ └── TextAlign.rb ├── 5KYU │ ├── ASCIIHex.rb │ ├── Anagram.rb │ ├── BestTravel.rb │ ├── BreakCamel.rb │ ├── Bulk.rb │ ├── Caesar.rb │ ├── CaesarCipher.rb │ ├── CalcFunc.rb │ ├── CamelCase.rb │ ├── CommDenom.rb │ ├── CountIP.rb │ ├── CrimboCal.rb │ ├── DeltaBits.rb │ ├── Direction.rb │ ├── DomainName.rb │ ├── Email.rb │ ├── FirstLet.rb │ ├── FiveDigit.rb │ ├── GreedGood.rb │ ├── GuessGift.rb │ ├── HashGen.rb │ ├── JoeNum.rb │ ├── JoeSurvivor.rb │ ├── LCM.rb │ ├── LuckCheck.rb │ ├── PigLatin.rb │ ├── Purchase.rb │ ├── RGBtoHEX.rb │ ├── ROT13.rb │ ├── RegexPass.rb │ ├── Reversi.rb │ ├── Robots.rb │ ├── Scramblies.rb │ ├── SnakeCase.rb │ ├── SortArray3.rb │ ├── SplitHash.rb │ ├── SumofPairs.rb │ ├── Tongues.rb │ ├── TripCheck.rb │ ├── TwoJoggers.rb │ ├── VaildParen.rb │ ├── hashtags.rb │ └── weight.rb ├── 6KYU │ ├── AlexJones.rb │ ├── ArrayDiff.rb │ ├── ArrayinArray.rb │ ├── BalanceArray.rb │ ├── BitCounting.rb │ ├── Bookseller.rb │ ├── BouncingBall.rb │ ├── ChangeMachine.rb │ ├── Collatz.rb │ ├── CountDuplicates.rb │ ├── Darts.rb │ ├── DeadAnts.rb │ ├── DeleteOccurance.rb │ ├── Div6.rb │ ├── Dubstep.rb │ ├── DuplicateEncoder.rb │ ├── FileMaster.rb │ ├── FindDivisors.rb │ ├── FindLetter.rb │ ├── FindOddInt.rb │ ├── FindOutlier.rb │ ├── FormatString.rb │ ├── FramedReflection.rb │ ├── GA-DE-RY-PO-LU-KI2.rb │ ├── GA-DE-RY-PO-LU-KI3.rb │ ├── Gloves.rb │ ├── GoodvEvil.rb │ ├── Grab.rb │ ├── GroupCommas.rb │ ├── Guess.rb │ ├── IDNum.rb │ ├── IP4.rb │ ├── IP4toInt32.rb │ ├── IQTest.rb │ ├── Kebabize.rb │ ├── LengthMissArray.rb │ ├── Linux1.rb │ ├── Linux2.rb │ ├── Linux3.rb │ ├── Linux4.rb │ ├── Linux5.rb │ ├── LongestPalindrome.rb │ ├── Luck.rb │ ├── MTV.rb │ ├── MasterFiles.rb │ ├── MinArray.rb │ ├── MorseCode.rb │ ├── Multiples3and5.rb │ ├── NextVersion.rb │ ├── NumBig.rb │ ├── NumExpand.rb │ ├── PalinDome.rb │ ├── Parent.rb │ ├── PassPhrase.rb │ ├── Pattern.rb │ ├── Pattern2.rb │ ├── Persistance.rb │ ├── PhoneNumber.rb │ ├── ProofRead.rb │ ├── RemoveURL.rb │ ├── ReverseWords.rb │ ├── Robot.rb │ ├── SalesmanTravel.rb │ ├── Same.rb │ ├── Search0.rb │ ├── Shell.rb │ ├── SimpleSentence.rb │ ├── SortArrays.rb │ ├── SpinWord.rb │ ├── SplitLoss.rb │ ├── SplitString.rb │ ├── SplitandJoin.rb │ ├── Square2.rb │ ├── StockBroker.rb │ ├── StreetFighter2.rb │ ├── StringCase.rb │ ├── StringRotate.rb │ ├── StringShort.rb │ ├── SumDigit.rb │ ├── SuperMan.rb │ ├── Temple.rb │ ├── TicTacToe.rb │ ├── TitleCase.rb │ ├── Tortoise.rb │ ├── Treasure.rb │ ├── TwistSub.rb │ ├── UniqueOrder.rb │ ├── ValidPhone.rb │ ├── VowelShift.rb │ ├── WhichAreIn.rb │ ├── WhoLikesIt.rb │ ├── WordInt.rb │ ├── WordPat.rb │ ├── Worda10n.rb │ └── upANDdown.rb ├── 7KYU │ ├── Ages.rb │ ├── AllStars28.rb │ ├── Anchorize.rb │ ├── ArrayIndex.rb │ ├── AverageScores.rb │ ├── Box.rb │ ├── BumpRoad.rb │ ├── CardMask.rb │ ├── CaseSwap.rb │ ├── CatandMouse.rb │ ├── CharCodeCal.rb │ ├── CloseTen.rb │ ├── ConvertStrArray.rb │ ├── Count.rb │ ├── CountChars.rb │ ├── CountLetandDig.rb │ ├── CountMarksQuestion.rb │ ├── Covfefe.rb │ ├── DNA.rb │ ├── DecOrder.rb │ ├── DivideConquar.rb │ ├── DontFive.rb │ ├── DropWhile.rb │ ├── Elements.rb │ ├── Exclamation.rb │ ├── FeqSeq.rb │ ├── FindVowels.rb │ ├── FizzBuzz.rb │ ├── FizzBuzzClock.rb │ ├── Flatten.rb │ ├── Flatten2.rb │ ├── FloatPrec.rb │ ├── Friday.rb │ ├── FriendorFoe.rb │ ├── Fruit.rb │ ├── GA-DE-RY-PO-LU-KI .rb │ ├── GradPara.rb │ ├── HeadTail.rb │ ├── HighProfit.rb │ ├── HighandLow.rb │ ├── Hit.rb │ ├── Ifs.rb │ ├── Incrementer.rb │ ├── InvalidInput.rb │ ├── IsEven.rb │ ├── Isograms.rb │ ├── JadenString.rb │ ├── KeeptheOrder.rb │ ├── Licks.rb │ ├── ListFiltering.rb │ ├── LuckyNum.rb │ ├── MakeMaths.rb │ ├── Marks.rb │ ├── MeanString.rb │ ├── Median.rb │ ├── MiddleChar.rb │ ├── Mumbling.rb │ ├── NaughtyNice.rb │ ├── Near5.rb │ ├── NewFiver.rb │ ├── NewMember.rb │ ├── NotOnTele.rb │ ├── Oddities.rb │ ├── Office1.rb │ ├── Office2.rb │ ├── Office3.rb │ ├── OldSwitcherroo.rb │ ├── Order.rb │ ├── PadNum.rb │ ├── Palindrones.rb │ ├── PartsOfList.rb │ ├── PrinterError.rb │ ├── RemoveMark.rb │ ├── RevChange.rb │ ├── ReverseLetter.rb │ ├── ReverseString.rb │ ├── RotateMax.rb │ ├── SayHello.rb │ ├── Scoring.rb │ ├── Scrabble.rb │ ├── SeaSick.rb │ ├── Socks.rb │ ├── SortChar.rb │ ├── Spaceship.rb │ ├── SplitParts.rb │ ├── Square.rb │ ├── SquareDigits.rb │ ├── StarCode.rb │ ├── StringDrill.rb │ ├── StringQuote.rb │ ├── StringSum.rb │ ├── StrtoInt.rb │ ├── SumDigits.rb │ ├── SumLowestTwo.rb │ ├── SumNumbers.rb │ ├── SumSequence.rb │ ├── SumString.rb │ ├── Suzuki.rb │ ├── SuzukiGarden.rb │ ├── Switcheroo.rb │ ├── Time.rb │ ├── TransString.rb │ ├── Triangle.rb │ ├── TrimString.rb │ ├── Trolls.rb │ ├── TwotoOne.rb │ ├── ValueArray.rb │ ├── Veg.rb │ ├── VowelCount.rb │ ├── VowelOne.rb │ └── XsandOs.rb ├── 8KYU │ ├── 2plus2.rb │ ├── AlterCase.rb │ ├── ArrayRemove.rb │ ├── ArraySecond.rb │ ├── BLost.rb │ ├── BReduce.rb │ ├── Banjo.rb │ ├── BasicFunction.rb │ ├── BasicVariables.rb │ ├── Bonus.rb │ ├── BoolString.rb │ ├── BoolYN.rb │ ├── BrokenGreetings.rb │ ├── ClassicHello.rb │ ├── ConvertArray.rb │ ├── ConvertDigits.rb │ ├── CountByX.rb │ ├── CountMonkeys.rb │ ├── CountPosNeg.rb │ ├── CrashOverride.rb │ ├── DNA.rb │ ├── DebugHello.rb │ ├── DecimalPlace.rb │ ├── DidSheSay.rb │ ├── Division.rb │ ├── DoubleChar.rb │ ├── DuckDuckGoose.rb │ ├── DutyFree.rb │ ├── EvenOdd.rb │ ├── FakeBinary.rb │ ├── Fighter.rb │ ├── FindDigit.rb │ ├── FixBugs.rb │ ├── Frog.rb │ ├── GetNum.rb │ ├── GrassCombine.rb │ ├── Grasshopper.rb │ ├── HelloWorld.rb │ ├── Hydrated.rb │ ├── IfElse.rb │ ├── IfLambda.rb │ ├── Jennys.rb │ ├── Last.rb │ ├── LoginBugFix.rb │ ├── Love.rb │ ├── MagicNum1.rb │ ├── MakeChange.rb │ ├── MaxandMin.rb │ ├── MeanOfArray.rb │ ├── Multiply.rb │ ├── Multiply2Num.rb │ ├── MyTail.rb │ ├── Needle.rb │ ├── NumtoString.rb │ ├── OnesandZeros.rb │ ├── OppNum.rb │ ├── OpposeiteAttract.rb │ ├── PalStrings.rb │ ├── Pirates.rb │ ├── Planet.rb │ ├── Plural.rb │ ├── Powerof2.rb │ ├── PreFizzBuzz.rb │ ├── RegBallSuperBall.rb │ ├── RemoveElements.rb │ ├── RemoveMark.rb │ ├── RemoveMarks.rb │ ├── RemoveSpaces.rb │ ├── Removechars.rb │ ├── ReplaceVowel.rb │ ├── ReturnHigh.rb │ ├── Reverse.rb │ ├── ReverseString.rb │ ├── RockPaperScissors.rb │ ├── Saber.rb │ ├── Sanity.rb │ ├── Sheep.rb │ ├── ShortLongShort.rb │ ├── SixToast.rb │ ├── Smash.rb │ ├── SoEasy.rb │ ├── SortandStar.rb │ ├── SplitArray.rb │ ├── SquareSum.rb │ ├── SquashBugs.rb │ ├── StringBugFix.rb │ ├── StringNumber.rb │ ├── StringRepeat.rb │ ├── StringUpcase.rb │ ├── StringyStrings.rb │ ├── SumArrays.rb │ ├── SumHighLow.rb │ ├── SumMixArray.rb │ ├── Summation.rb │ ├── SumofPositive.rb │ ├── SwitchItUp.rb │ ├── TerminalGame.rb │ ├── UEFA.rb │ ├── Vacation.rb │ ├── VolumeCuboid.rb │ ├── VowelInThere.rb │ ├── VowelRemove.rb │ ├── WelcomeCity.rb │ ├── WellofIdeas.rb │ ├── XOR.rb │ └── YieldBlock.rb ├── Authored │ ├── 4KYU.png │ ├── 5KYU.png │ ├── 6KYU.png │ ├── 7KYU.png │ ├── 8KYU.png │ ├── Beta.png │ ├── CarPark.rb │ ├── Connect4.rb │ ├── Driving.rb │ ├── Drunk.rb │ ├── Fish.rb │ ├── Fruit.rb │ ├── GuessWho.rb │ ├── Hippo.rb │ ├── Mastermind.rb │ ├── MazeRunner.rb │ ├── ScoobyDoo.rb │ ├── SnakesLadders.rb │ ├── Square.rb │ ├── Square1.png │ ├── Square2.png │ ├── Square3.png │ ├── SquareExample.png │ ├── Uno.rb │ ├── VendingMachine.rb │ ├── Wave.png │ ├── Wave.rb │ ├── atom.png │ ├── c.png │ ├── carpark.jpg │ ├── codewars.png │ ├── connect4.jpg │ ├── cpp.png │ ├── crystal.png │ ├── csharp.png │ ├── demo.png │ ├── drivinglicense.jpg │ ├── drunk.png │ ├── fish.jpg │ ├── fruit.jpg │ ├── fsharp.png │ ├── github.png │ ├── guesswho.png │ ├── guesswho_banner.jpg │ ├── haskell.png │ ├── heroku.png │ ├── hippo.jpg │ ├── hippo.png │ ├── java.png │ ├── javascript.png │ ├── mastermind.jpg │ ├── mastermind1.png │ ├── mastermind2.png │ ├── mazerunner.jpg │ ├── php.png │ ├── python.png │ ├── ruby.png │ ├── scoobydoopuzzle.jpg │ ├── snakesandladders.jpg │ ├── snakesandladdersboard.jpg │ ├── sql.png │ ├── squares_banner.jpg │ ├── typescript.png │ └── vendingmachine.jpg ├── Translation │ ├── AlienAccent.rb │ ├── AllStar02.rb │ ├── AllStar03.rb │ ├── AllStar05.rb │ ├── AllStar06.rb │ ├── AllStar13.rb │ ├── AllStar15.rb │ ├── AllStar18.rb │ ├── AllStar22.rb │ ├── BattleShips.rb │ ├── Bee.rb │ ├── BowlingPins.rb │ ├── Collatz.rb │ ├── CountZeros.rb │ ├── GuessGame.rb │ ├── Hangman.rb │ ├── Lottery.rb │ ├── NoYelling.rb │ ├── Question.rb │ ├── SeriesInt.rb │ ├── SeriesMtoN.rb │ ├── ShoppingList.rb │ ├── StreetFighter2.rb │ ├── SumNum.rb │ ├── Switch.rb │ ├── WordCount.rb │ └── Worrying.rb ├── beta │ ├── 123.rb │ ├── AddWord2.rb │ ├── AlexMack.rb │ ├── AllStar16.rb │ ├── Anagram.rb │ ├── ArrayExchange.rb │ ├── Bath.rb │ ├── Bowling.rb │ ├── CamelCase.rb │ ├── CarSaleman.rb │ ├── Compress.rb │ ├── ConvertSymArray.rb │ ├── CountLetter.rb │ ├── FilmLibrary1.rb │ ├── FilmLibrary2.rb │ ├── FilmLibrary3.rb │ ├── FindArray.rb │ ├── FraggleRock.rb │ ├── Friday.rb │ ├── Golden.rb │ ├── HelloWorld.rb │ ├── HighValue.rb │ ├── Isogram.rb │ ├── KeepHoop.rb │ ├── NdivXandY.rb │ ├── Phonetic.rb │ ├── Pirate.rb │ ├── Plural.rb │ ├── RobotWalk.rb │ ├── SchoolPaperword.rb │ ├── SevenAte9.rb │ ├── SimpleMaths.rb │ ├── Spammy.rb │ ├── TerminalCombat.rb │ ├── YesNo.rb │ └── snooker.rb └── ruby.md └── SQL ├── 8KYU ├── Active.sql ├── Distinct.sql ├── Grasshopper.sql └── Order.sql ├── BETA └── Adults.sql └── SQL.md /CSharp/5KYU/CamelCase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | public class Kata 5 | { 6 | public static string ToCamelCase(string str) 7 | { 8 | if (str == "") { return ""; } 9 | char[] letters = str.ToCharArray(); 10 | List result = new List(); 11 | bool camel = false; 12 | result.Add(letters[0]); 13 | for (int x=1; x < letters.Length; x++) 14 | { 15 | if (letters[x]=='_' || letters[x] == '-') 16 | { 17 | camel = true; 18 | } 19 | else 20 | { 21 | if (camel) { result.Add(letters[x] = Char.ToUpper(letters[x])); } else { result.Add(letters[x]); } 22 | camel = false; 23 | } 24 | } 25 | return string.Join("",result); 26 | } 27 | } -------------------------------------------------------------------------------- /CSharp/6KYU/BuildTower.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static string[] TowerBuilder(int nFloors) 4 | { 5 | string[] result = new string[nFloors]; 6 | int len = (nFloors - 1) * 2 + 1; 7 | for(int x=0; x nums = new Dictionary(); 9 | for (int x=0; x entry in nums) 14 | { 15 | if (entry.Value % 2 != 0) { return entry.Key; } 16 | } 17 | return -1; 18 | } 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /CSharp/6KYU/GA-DE-RY-PO-LU-KI2.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static string Encode(string str, string key) 4 | { 5 | return EncodeDecode(str, key); 6 | } 7 | 8 | public static string Decode(string str, string key) 9 | { 10 | return EncodeDecode(str, key); 11 | } 12 | 13 | private static string EncodeDecode(string str, string key) 14 | { 15 | key = key + key.ToUpper(); 16 | for (int i = 0; i < key.Length; i+=2) 17 | { 18 | str = str.Replace(key[i], '~').Replace(key[i+1], key[i]).Replace('~', key[i+1]); 19 | } 20 | return str; 21 | } 22 | } -------------------------------------------------------------------------------- /CSharp/6KYU/Multiples3and5.cs: -------------------------------------------------------------------------------- 1 | public static class Kata 2 | { 3 | public static int Solution(int value) 4 | { 5 | int result = 0; 6 | for (int x=1; x= key.Length) { num = 0; } 13 | } 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /CSharp/7KYU/Cypher2.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static string Decode(int[] code, int n) 4 | { 5 | var key = n.ToString().ToCharArray(); 6 | var num = 0; 7 | char[] result = new char[code.Length]; 8 | for (int i = 0; i < code.Length; i++) 9 | { 10 | result[i] = (char)(code[i]+96- int.Parse(key[num].ToString())); 11 | num++; 12 | if (num >= key.Length) { num = 0; } 13 | } 14 | return new string(result); 15 | } 16 | } -------------------------------------------------------------------------------- /CSharp/7KYU/Histogram.cs: -------------------------------------------------------------------------------- 1 | public class Dinglemouse 2 | { 3 | public static string Histogram(int[] results) 4 | { 5 | string ReturnString = ""; 6 | for (int x = results.Length; x > 0; x--) 7 | { 8 | ReturnString += x.ToString() + "|"; 9 | if (results[x - 1] != 0) 10 | { 11 | for (int y = 0; y < results[x - 1]; y++) 12 | { 13 | ReturnString += "#"; 14 | } 15 | ReturnString += " " + (results[x - 1]).ToString(); 16 | } 17 | ReturnString += "\n"; 18 | } 19 | return ReturnString; 20 | } 21 | } -------------------------------------------------------------------------------- /CSharp/7KYU/Lamps.cs: -------------------------------------------------------------------------------- 1 | namespace myjinxin 2 | { 3 | using System; 4 | 5 | public class Kata 6 | { 7 | public int Lamps(int[] a){ 8 | int amount1 = 0; int amount2 = 0; 9 | for (int x=0; x", string.Empty); 10 | return ""; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /CSharp/8KYU/ab.cs: -------------------------------------------------------------------------------- 1 | public class FirstClass 2 | { 3 | public static byte sum (byte a, byte b) 4 | { 5 | byte c = (byte)(a + b); 6 | return c; 7 | } 8 | } -------------------------------------------------------------------------------- /CSharp/8KYU/bindec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Solution 4 | { 5 | public static class Program 6 | { 7 | public static int binToDec(string s) 8 | { 9 | return Convert.ToInt32(s, 2); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /CSharp/8KYU/bus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | public class Kata 5 | { 6 | public static int Number(List peopleListInOut) 7 | { 8 | int result = 0; 9 | foreach (int[] a in peopleListInOut) { result += a[0]; result -= a[1];} 10 | return result; 11 | } 12 | } -------------------------------------------------------------------------------- /CSharp/8KYU/enumerable.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static bool Include(int[] arr, int item) 4 | { 5 | for (int x=0; x x* -1).ToArray(); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /CSharp/8KYU/loop.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | public class Kata 5 | { 6 | public static List CreateList(int number) 7 | { 8 | List list = new List(); 9 | 10 | for(int counter = 1; counter <= number; counter++) 11 | { 12 | list.Add(counter); 13 | } 14 | 15 | return list; 16 | } 17 | } -------------------------------------------------------------------------------- /CSharp/8KYU/multiply.cs: -------------------------------------------------------------------------------- 1 | public class CustomMath { 2 | public static int multiply(int a, int b) { 3 | return a * b; 4 | } 5 | } -------------------------------------------------------------------------------- /CSharp/8KYU/rps.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text.RegularExpressions; 5 | 6 | public class Kata 7 | { 8 | public string Rps(string p1, string p2) 9 | { 10 | if (p1=="rock" && p2=="scissors"){return "Player 1 won!";} 11 | if (p1=="paper" && p2=="rock"){return "Player 1 won!";} 12 | if (p1=="scissors" && p2=="paper"){return "Player 1 won!";} 13 | 14 | if (p2=="rock" && p1=="scissors"){return "Player 2 won!";} 15 | if (p2=="paper" && p1=="rock"){return "Player 2 won!";} 16 | if (p2=="scissors" && p1=="paper"){return "Player 2 won!";} 17 | return "Draw!"; 18 | } 19 | } -------------------------------------------------------------------------------- /CSharp/8KYU/squareroot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | public class Kata 3 | { 4 | public static int[] SquareOrSquareRoot(int[] array) 5 | { 6 | for (int x=0; x< array.Length; x++) 7 | { 8 | if (Math.Sqrt(array[x]) % 1 == 0) { array[x] = Convert.ToInt32(Math.Sqrt(array[x])); } else { array[x] = array[x] * array[x]; } 9 | } 10 | return array; 11 | } 12 | } 13 | 14 | 15 | 16 | using System; 17 | using System.Linq; 18 | 19 | public class Kata 20 | { 21 | public static int[] SquareOrSquareRoot(int[] array) 22 | { 23 | return array.Select(x => (int)(Math.Sqrt(x) % 1 == 0 ? Math.Sqrt(x) : x * x)).ToArray(); 24 | } 25 | } -------------------------------------------------------------------------------- /CSharp/8KYU/subsum.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static string SubtractSum(int number) 4 | { 5 | return "apple"; 6 | } 7 | } -------------------------------------------------------------------------------- /CSharp/8KYU/swap.cs: -------------------------------------------------------------------------------- 1 | public class Swapper 2 | { 3 | public object[] Arguments { get; private set; } 4 | 5 | public Swapper(object[] args) 6 | { 7 | Arguments = args; 8 | } 9 | 10 | public void SwapValues() 11 | { 12 | object temp1 = Arguments[0]; 13 | Arguments[0] = Arguments[1]; 14 | Arguments[1] = temp1; 15 | } 16 | } -------------------------------------------------------------------------------- /CSharp/8KYU/switch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Kata 4 | { 5 | public static double EvalObject(double value1, double value2, char operation) 6 | { 7 | double result = 0; 8 | switch(operation) 9 | { 10 | case '+': result = value1 + value2; break; 11 | case '-': result = value1 - value2; break; 12 | case '/': result = value1 / value2; break; 13 | case '*': result = value1 * value2; break; 14 | case '%': result = value1 % value2; break; 15 | case '^': result = Math.Pow(value1, value2); break; 16 | } 17 | return result; 18 | } 19 | } -------------------------------------------------------------------------------- /CSharp/8KYU/theatre.cs: -------------------------------------------------------------------------------- 1 | public class Kata 2 | { 3 | public static int SeatsInTheater(int nCols, int nRows, int col, int row) 4 | { 5 | return ((nCols - (col-1)) * (nRows - row)); 6 | } 7 | } -------------------------------------------------------------------------------- /CSharp/Beta/Decimal.cs: -------------------------------------------------------------------------------- 1 | public class DecTools { 2 | public static int Digits(ulong n) { 3 | return n.ToString().Length; 4 | } 5 | } -------------------------------------------------------------------------------- /CSharp/Beta/MissingLetters.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Kata 4 | { 5 | public class WordGuesser 6 | { 7 | public int CountCorrectCharacters(string correctWord, string guess) 8 | { 9 | if(correctWord.Length!=guess.Length){throw new System.InvalidOperationException("ERROR");} 10 | int result = 0; 11 | for(int i=0; i -1) { 5 | text[i] = "*".repeat(text[i].length); 6 | } 7 | } 8 | return text.join(" "); 9 | } -------------------------------------------------------------------------------- /Javascript/6KYU/305.js: -------------------------------------------------------------------------------- 1 | function typist(s){ 2 | n=0; s=s.split(""); lock = false; 3 | for(i=0;i1 && hive[x][y]=="b"){if (hive[x-1][y]=="e" && hive[x-2][y]=="e"){result++;}} 7 | if (x1 && hive[x][y]=="b"){if (hive[x][y-1]=="e" && hive[x][y-2]=="e"){result++;}} 9 | if (y 1){isit = false} 7 | } 8 | if (isit){return parseInt(t);} 9 | } 10 | return -1; 11 | } -------------------------------------------------------------------------------- /Javascript/6KYU/Range1.js: -------------------------------------------------------------------------------- 1 | function range(...n){ 2 | result = [] 3 | stop = n[n.length-1] 4 | n.length > 1 ? start = n[0] : start = 1 5 | n.length > 2 ? step = n[1] : step = 1 6 | for(i=start;i<=stop;i+=step){result.push(i);} 7 | return result; 8 | } -------------------------------------------------------------------------------- /Javascript/6KYU/Range2.js: -------------------------------------------------------------------------------- 1 | function range(...n) { 2 | result = []; 3 | step = 1; 4 | stop = n[n.length-1] 5 | n.length > 1 ? start = n[0] : start = 0 6 | if (n.length > 2){ step = n[2], stop = n[1];} 7 | if(step===0){return [start, start, start]} 8 | for(i=start;i 96 && val < 123){result.push(val-96)}; 6 | } 7 | return result.join(" "); 8 | } -------------------------------------------------------------------------------- /Javascript/6KYU/Select.js: -------------------------------------------------------------------------------- 1 | function selReverse(array, length) { 2 | if(length===0){return array} 3 | result = []; 4 | for(x=0;x=x;y--){if (array[y]!==undefined){result.push(array[y]);}}} 5 | return result; 6 | } -------------------------------------------------------------------------------- /Javascript/6KYU/SortOdd.js: -------------------------------------------------------------------------------- 1 | function sortArray(array) { 2 | temp = [] 3 | for(i=0;i a-b); on = 0 5 | for(i=0;i 1){ 3 | str = str.split("") 4 | temp = 0; 5 | for(i=0;i len){result=array[x];len=array[x].toString().length}} 4 | return result; 5 | } -------------------------------------------------------------------------------- /Javascript/7KYU/Even.js: -------------------------------------------------------------------------------- 1 | function evenChars(string) { 2 | if(string.length<2 || string.length>100){return "invalid string";} 3 | result = []; 4 | for(i=1;i=0;i--){result += this[i];} 4 | return result; 5 | } -------------------------------------------------------------------------------- /Javascript/7KYU/Flatten.js: -------------------------------------------------------------------------------- 1 | function flattenAndSort(array) { 2 | return [].concat.apply([], array).sort(function(a,b){return a - b}); 3 | } -------------------------------------------------------------------------------- /Javascript/7KYU/Gift.js: -------------------------------------------------------------------------------- 1 | names = []; 2 | 3 | function handOutGift(name) { 4 | if (names.indexOf(name) > -1) {throw "ERROR";} 5 | names.push(name); 6 | } -------------------------------------------------------------------------------- /Javascript/7KYU/Grey.js: -------------------------------------------------------------------------------- 1 | var color2grey = function (image) { 2 | result = []; 3 | for(a=0;ab): return a.toString() + " is greater than " + b.toString(); break; 5 | case (ab.length){num=a.length}else{num=b.length} 4 | for(i=0;i= result){result = amount;} 7 | } else { 8 | amount = 0; 9 | } 10 | } 11 | return result; 12 | }; -------------------------------------------------------------------------------- /Javascript/7KYU/OnlyOne.js: -------------------------------------------------------------------------------- 1 | function onlyOne() { 2 | result = {true:0, false:0} 3 | for(i=0;i a.length - b.length); 3 | }; -------------------------------------------------------------------------------- /Javascript/7KYU/SplitCase.js: -------------------------------------------------------------------------------- 1 | function splitter(str){ 2 | result = str[0] || ""; 3 | for(i=1;i a + b, 0) / array.length; 6 | } 7 | -------------------------------------------------------------------------------- /Javascript/8KYU/Clean.js: -------------------------------------------------------------------------------- 1 | function stringClean(s){ 2 | return s.replace(/\d/g, ""); 3 | } -------------------------------------------------------------------------------- /Javascript/8KYU/First.js: -------------------------------------------------------------------------------- 1 | function firstNonConsecutive (arr) { 2 | for (x=1; x [...Array(n)].map((x, i) => i+1); 26 | -------------------------------------------------------------------------------- /Javascript/8KYU/Fold.js: -------------------------------------------------------------------------------- 1 | function foldTo(distance) { 2 | num = 0.0001; result = 0 3 | while(num < distance){ result++; num = num * 2;} 4 | return distance <= 0 ? null : result; 5 | } -------------------------------------------------------------------------------- /Javascript/8KYU/Guess.js: -------------------------------------------------------------------------------- 1 | class Guesser { 2 | constructor(number, lives) { 3 | this.number = number; 4 | this.lives = lives; 5 | } 6 | 7 | guess(n) { 8 | if (this.lives < 1) throw("already dead"); 9 | if (n === this.number) return true; 10 | 11 | this.lives-- 12 | return false; 13 | } 14 | } -------------------------------------------------------------------------------- /Javascript/8KYU/MaxHeadRoom.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Shouldn't the two functions getMax1 and getMax2 be equivalent and return the 4 | same value? Can you spot a problem and fix it? Can you learn something about 5 | JavaScript style in this kata? 6 | 7 | */ 8 | 9 | // My Solution 10 | function getMax1() 11 | { 12 | var max = 13 | { 14 | name: 'Max Headroom' 15 | } 16 | return max; 17 | } 18 | 19 | function getMax2() 20 | { 21 | var max = 22 | { 23 | name: 'Max Headroom' 24 | } 25 | return max; 26 | } 27 | 28 | // Better Solution 29 | function getMax1() { return {name: 'Max Headroom'}; } 30 | 31 | function getMax2() { return getMax1(); } 32 | -------------------------------------------------------------------------------- /Javascript/8KYU/Nice.js: -------------------------------------------------------------------------------- 1 | function getAge(inputString){ 2 | return parseInt(inputString[0]); 3 | } -------------------------------------------------------------------------------- /Javascript/Authored/Fish.js: -------------------------------------------------------------------------------- 1 | function fish(shoal){ 2 | level = 1; 3 | total = 0; 4 | fishes = shoal.split("").sort(); 5 | fishes.forEach(function(fish) { 6 | if (parseInt(fish) <= level){ 7 | total += (1*parseInt(fish)); 8 | if (total >= (level*4)){ 9 | total -= (level*4) 10 | level++; 11 | } 12 | } 13 | }); 14 | return total; 15 | } 16 | -------------------------------------------------------------------------------- /Javascript/Authored/Fruit.js: -------------------------------------------------------------------------------- 1 | function fruit(reels, spins){ 2 | reel = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"]; 3 | items = [reels[0][spins[0]], reels[1][spins[1]], reels[2][spins[2]]]; 4 | if (items[0] == items[1] && items[0] == items[2]){return (10 - reel.indexOf(items[0])) * 10;} 5 | item = ""; extra = ""; 6 | if (items[0] == items[1]){item = items[0]; extra = items[2];} 7 | if (items[0] == items[2]){item = items[0]; extra = items[1];} 8 | if (items[1] == items[2]){item = items[1]; extra = items[0];} 9 | if (item != ""){ 10 | num = 10 - reel.indexOf(item) 11 | if (extra == "Wild"){num = num * 2;} 12 | return num; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Javascript/Authored/Wave.js: -------------------------------------------------------------------------------- 1 | function wave(){ 2 | result = []; 3 | str = str.split(""); 4 | for(b=0;bb.length){return "greater";} 4 | if(a.lengthb){return "greater";} 6 | return "less"; 7 | } -------------------------------------------------------------------------------- /Javascript/Beta/3Root.js: -------------------------------------------------------------------------------- 1 | function perfectRoots(n){ 2 | for(i=0;i<3;i++){n = Math.sqrt(n);} 3 | return n % 1 === 0 4 | } -------------------------------------------------------------------------------- /Javascript/Beta/Alternate.js: -------------------------------------------------------------------------------- 1 | function isAlt(word) { 2 | on = 2 3 | for(i=0;i 192827000000 10 | // 11 | // 938473 --> 938000 12 | // 13 | // 2837743 --> 2830000 14 | 15 | // My Solution 16 | function manipulate(num) { 17 | var result = ""; 18 | var numWord = num.toString().split(""); 19 | for(var i=0; i < Math.floor(numWord.length/2); i++){ 20 | result += numWord[i]; 21 | } 22 | for(i=0; i < Math.ceil(numWord.length/2); i++){ 23 | result += "0"; 24 | } 25 | return parseInt(result); 26 | } 27 | -------------------------------------------------------------------------------- /Javascript/Beta/Merge.js: -------------------------------------------------------------------------------- 1 | function mergeArrays(arr1, arr2) { 2 | result = []; 3 | for(i=0;i a-b); 6 | } -------------------------------------------------------------------------------- /Javascript/Beta/NextLine.js: -------------------------------------------------------------------------------- 1 | // Several people are waiting in a long queue for the theater. Write a function 2 | // that returns a name from a position in the queue. 3 | // 4 | // You will not be given any out-of-range (queue position 0) or null values. 5 | // 6 | // Examples: 7 | // 8 | // queue = ["Sally", "Jim", "Bob", "Kate"]; 9 | // 10 | // findInQueue(queue, 1); --> Returns "Sally" 11 | // 12 | // findInQueue(queue, 3); --> Returns "Bob" 13 | // 14 | // My Solution 15 | class Kata { 16 | static findInQueue(queue,n) { 17 | return queue[n-1]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Javascript/Beta/NorthSouth.js: -------------------------------------------------------------------------------- 1 | function box(coords) { 2 | var cornors={nw:[0,0], se:[0,0]}; 3 | for (x=0; x cornors['nw'][0]) {cornors['nw'][0] = coords[x][0]}; 5 | if (coords[x][0] < cornors['se'][0]) {cornors['se'][0] = coords[x][0]}; 6 | if (coords[x][1] < cornors['nw'][1]) {cornors['nw'][1] = coords[x][1]}; 7 | if (coords[x][1] > cornors['se'][1]) {cornors['se'][1] = coords[x][1]}; 8 | } 9 | return cornors; 10 | } -------------------------------------------------------------------------------- /Javascript/Beta/Password.js: -------------------------------------------------------------------------------- 1 | function getPassword(grid, directions) { 2 | xpos=0;ypos=0;result=""; 3 | for (x=0;x=0;i--){ 20 | for(g=0;g=1;x--){temp.push(x%10);} 8 | result.push(temp.join(" ")); 9 | } 10 | return result.join("\n"); 11 | } -------------------------------------------------------------------------------- /Javascript/Beta/WhoAmI.js: -------------------------------------------------------------------------------- 1 | // Write a function that returns the type of its input. 2 | 3 | // My Solution 4 | function checkType(value) { 5 | return typeof value; 6 | } 7 | -------------------------------------------------------------------------------- /Javascript/Beta/fish.js: -------------------------------------------------------------------------------- 1 | function fish(arr){ 2 | if(arr.length===1){return "no catch";} 3 | var result = {}; 4 | for(var i = 0; i< arr.length; i++) {result[arr[i]] = result[arr[i]] ? result[arr[i]]+1 : 1;} 5 | for(var key in result) {if(result[key]===1){return key;}} 6 | return "no catch"; 7 | } 8 | 9 | x = fish(["red herring","blue herring","yellow herring"]) 10 | 11 | console.log(x) 12 | -------------------------------------------------------------------------------- /PHP/7KYU/Jenny.php: -------------------------------------------------------------------------------- 1 | function missing($nums, $str) { 2 | $result = ""; 3 | sort($nums); 4 | $temp = implode("",(explode(" ",$str))); 5 | for ($x = 0; $x < 3; $x++) { 6 | if ($nums[$x] >= strlen($temp)){ 7 | return "No mission today"; 8 | } 9 | $result .= strtolower($temp[$nums[$x]]); 10 | } 11 | return $result; 12 | } -------------------------------------------------------------------------------- /PHP/8KYU/Fold.php: -------------------------------------------------------------------------------- 1 | function fold_to($distance) { 2 | $num = 0.0001; 3 | $result = 0; 4 | while($num < $distance){ 5 | $result++; 6 | $num = $num * 2; 7 | } 8 | return $distance <= 0 ? null : $result; 9 | } -------------------------------------------------------------------------------- /PHP/PHP.md: -------------------------------------------------------------------------------- 1 | # [![CodeWars](https://raw.githubusercontent.com/adrianeyre/codewars/master/Ruby/Authored/php.png)](PHP/PHP.md) PHP 2 | 3 | [8 KYU](#8KYU) | [7 KYU](#7KYU) | [6 KYU](#6KYU) | [5 KYU](#5KYU) | [4 KYU](#4KYU) | [3 KYU](#3KYU) | [2 KYU](#2KYU) | [1 KYU](#1KYU) | [BETA](#BETA) | [Translations](#TRAN) | [Authored](#AUTH) 4 | 5 | ### 8 KYU 6 | * [Folding your way to the moon](8KYU/Fold.php) - [CodeWars](https://www.codewars.com/kata/58f0ba42e89aa6158400000e) 7 | 8 | ### 7 KYU 9 | * [Jenny the youngest detective](7KYU/Jenny.php) - [CodeWars](https://www.codewars.com/kata/58b972cae826b960a300003e) 10 | -------------------------------------------------------------------------------- /Python/3KYU/Calc.py: -------------------------------------------------------------------------------- 1 | class Calculator(object): 2 | def evaluate(self, string): 3 | return round(eval(string), 12) -------------------------------------------------------------------------------- /Python/5KYU/Bulk.py: -------------------------------------------------------------------------------- 1 | def bulk(arr): 2 | p = 0; c = 0 3 | if arr!=[]: 4 | for i in range(0,len(arr)): 5 | items = arr[i].split(",") 6 | for item in range(0,len(items)): 7 | unit = items[item].split("g ") 8 | units = int(unit[0])/100 9 | protein = units * food[unit[1]][0] 10 | carbohydrate = units * food[unit[1]][1] 11 | fat = units * food[unit[1]][2] 12 | p += protein 13 | c += (protein * 4) + (carbohydrate * 4) + (fat * 9) 14 | if p % 1 == 0: p = int(p) 15 | if c % 1 == 0: c = int(c) 16 | return "Total proteins: "+str(p)+" grams, Total calories: "+str(c) -------------------------------------------------------------------------------- /Python/6KYU/FizzBuzz.py: -------------------------------------------------------------------------------- 1 | def solution(number): 2 | a=0;b=0;c=0 3 | for x in range(1,number): 4 | if x%15==0: 5 | c += 1 6 | elif x%5==0: 7 | b += 1 8 | elif x%3==0: 9 | a += 1 10 | return [a,b,c] -------------------------------------------------------------------------------- /Python/6KYU/GA-DE-RY-PO-LU-KI2.py: -------------------------------------------------------------------------------- 1 | def encode(str, key): return ende(str, key) 2 | def decode(str, key): return ende(str, key) 3 | 4 | def ende(str, key): 5 | key+=key.upper() 6 | rkey = "" 7 | for l in range(0,len(key),2): 8 | rkey+=key[l+1]+key[l] 9 | str = list(str) 10 | for i in range(0,len(str)): 11 | if str[i] in key: 12 | str[i] = rkey[list(key).index(str[i])] 13 | return "".join(str) -------------------------------------------------------------------------------- /Python/6KYU/Order.py: -------------------------------------------------------------------------------- 1 | def order(sentence): 2 | result = [] 3 | sentence = sentence.split(" ") 4 | for i in range(1,len(sentence)+1): 5 | for a in range(0,len(sentence)): 6 | if str(i) in sentence[a]: result.append(sentence[a]) 7 | return " ".join(result) -------------------------------------------------------------------------------- /Python/6KYU/Replace.py: -------------------------------------------------------------------------------- 1 | def alphabet_position(text): 2 | result = [] 3 | for i in text: 4 | val = ord(i.lower()) 5 | if val > 96 and val < 123: 6 | result.append(str(val-96)) 7 | return " ".join(result) -------------------------------------------------------------------------------- /Python/6KYU/Treasure.py: -------------------------------------------------------------------------------- 1 | def unlock(message): return message.lower().translate(message.maketrans("abcdefghijklmnopqrstuvwxyz","22233344455566677778889999")) -------------------------------------------------------------------------------- /Python/7KYU/Check.py: -------------------------------------------------------------------------------- 1 | def make_checkered_board(n): 2 | result = [] 3 | for x in range(0,n): 4 | temp = [] 5 | for y in range(0,n): 6 | temp.append("X") if (x+y) % 2 == 0 else temp.append("O") 7 | result.append(temp) 8 | return result -------------------------------------------------------------------------------- /Python/7KYU/Flatten.py: -------------------------------------------------------------------------------- 1 | def flatten_and_sort(array): 2 | return sorted([item for sublist in array for item in sublist]) -------------------------------------------------------------------------------- /Python/7KYU/GA-DE-RY-PO-LU-KI.py: -------------------------------------------------------------------------------- 1 | def encode(str): return ende(str) 2 | def decode(str): return ende(str) 3 | 4 | def ende(str): 5 | code = ["gaderypolukiGADERYPOLUKI","agedyropulikAGEDYROPULIK"] 6 | str = list(str) 7 | for i in range(0,len(str)): 8 | if str[i] in code[0]: 9 | str[i] = code[1][list(code[0]).index(str[i])] 10 | return "".join(str) -------------------------------------------------------------------------------- /Python/7KYU/GuessNum.py: -------------------------------------------------------------------------------- 1 | def guess_my_number(guess, number = '123-451-2345'): 2 | number = list(number) 3 | guess = list(guess) 4 | for num in range(0, len(number)): 5 | if not number[num] in guess and number[num] != "-": 6 | number[num] = "#" 7 | return "".join(number) -------------------------------------------------------------------------------- /Python/7KYU/ISEven.py: -------------------------------------------------------------------------------- 1 | def is_even(n): return n & 1 == 0 -------------------------------------------------------------------------------- /Python/7KYU/Ifs.py: -------------------------------------------------------------------------------- 1 | def no_ifs_no_buts(a, b): 2 | return { 3 | a == b: str(a) + " is equal to " + str(b), 4 | a < b: str(a) + " is smaller than " + str(b), 5 | a > b: str(a) + " is greater than " + str(b), 6 | }[True] -------------------------------------------------------------------------------- /Python/7KYU/Sheep.py: -------------------------------------------------------------------------------- 1 | def lostSheep(friday,saturday,total): 2 | return total - sum(friday) - sum(saturday) -------------------------------------------------------------------------------- /Python/7KYU/Spend.py: -------------------------------------------------------------------------------- 1 | def getTotal(costs, items, tax): 2 | total = 0 3 | for i in items: 4 | if i in costs: 5 | total += costs[i] 6 | return round(total*tax+total, 2) -------------------------------------------------------------------------------- /Python/7KYU/Spot.py: -------------------------------------------------------------------------------- 1 | def spot_diff(s1, s2): 2 | result = [] 3 | for a in range(0,len(s1)): 4 | if s1[a] != s2[a]: result.append(a) 5 | return result -------------------------------------------------------------------------------- /Python/8KYU/FindNumber.py: -------------------------------------------------------------------------------- 1 | def find_multiples(integer, limit): 2 | result = [] 3 | for x in range(integer, limit+1, integer): 4 | result.append(x) 5 | return result -------------------------------------------------------------------------------- /Python/8KYU/N.py: -------------------------------------------------------------------------------- 1 | import math 2 | def index(array, n): 3 | if n > len(array): 4 | return -1; 5 | return int(math.pow(array[n], n)) -------------------------------------------------------------------------------- /Python/Authored/Fish.py: -------------------------------------------------------------------------------- 1 | def fish(shoal): 2 | level = 1 3 | total = 0 4 | fishes = sorted(list(shoal)) 5 | for fish in fishes: 6 | if int(fish) <= level: 7 | total += (1*int(fish)) 8 | if total >= (level*4): 9 | total -= (level*4) 10 | level += 1 11 | return level 12 | -------------------------------------------------------------------------------- /Python/Authored/Fruit.py: -------------------------------------------------------------------------------- 1 | def fruit(reels, spins): 2 | reel = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"] 3 | items = [reels[0][spins[0]]][0], [reels[1][spins[1]]][0], [reels[2][spins[2]]][0] 4 | if items[0] == items[1] and items[0] == items[2]: return (10 - reel.index(items[0])) * 10 5 | item = ""; extra = "" 6 | if items[0] == items[1]: item = items[0]; extra = items[2] 7 | if items[0] == items[2]: item = items[0]; extra = items[1] 8 | if items[1] == items[2]: item = items[1]; extra = items[0] 9 | if item != "": 10 | num = 10 - reel.index(item) 11 | if extra == "Wild": num = num * 2 12 | return num 13 | return 0 14 | -------------------------------------------------------------------------------- /Python/Authored/Wave.py: -------------------------------------------------------------------------------- 1 | def wave(str): 2 | result = [] 3 | str = list(str) 4 | for b in range(0,len(str)): 5 | if str[b] != " ": 6 | temp = [] 7 | for i in range(0, len(str)): 8 | if i == b: 9 | temp.append(str[i].upper()) 10 | else: 11 | temp.append(str[i]) 12 | result.append("".join(temp)) 13 | return result 14 | 15 | import random 16 | letters = list("abcd efghi jklmno pqrstu vwxyz") 17 | for cwtests in range (1,2): 18 | word = [] 19 | for wordloop in range(0,random.randint(0,200)): 20 | word.append(letters[random.randint(0,len(letters)-1)]) 21 | word = "".join(word) 22 | print(word) 23 | -------------------------------------------------------------------------------- /Python/Authored/scooby.py: -------------------------------------------------------------------------------- 1 | def scoobydoo(villian, villians): 2 | villian = villian.lower() 3 | 4 | # Step 1 - Rotate Right by 5 5 | villian = ''.join(shift(villian, 5)) 6 | 7 | # Step 2 - Reverse 8 | villian = villian[::-1] 9 | 10 | # Step 3 - Change even letters by 5 11 | badguy = "" 12 | num = 0 13 | for le in villian: 14 | num += 1 15 | if num % 2 == 0: 16 | le = le.translate(str.maketrans('abcdefghijklmnopqrstuvwxyz','fghijklmnopqrstuvwxyzabcde')) 17 | badguy += le 18 | 19 | for bg in villians: 20 | if ''.join(bg.lower().split(' ')) == badguy: return bg 21 | -------------------------------------------------------------------------------- /Python/Beta/2Numbers.py: -------------------------------------------------------------------------------- 1 | # You will be provided with 3 numbers. num1, num2 and a target number target. 2 | # Your job is to write a program that will return the correct operator to get 3 | # num1 and num2 to the target variable. 4 | # 5 | # It can return either: add, subtract, multiply or divide appropriately. 6 | 7 | # My Solution 8 | def get_op(num1, num2, target): 9 | if num1 + num2 == target: return "add" 10 | if num1 - num2 == target: return "subtract" 11 | if num1 * num2 == target: return "multiply" 12 | return "divide" 13 | -------------------------------------------------------------------------------- /Python/Beta/Div.py: -------------------------------------------------------------------------------- 1 | import math 2 | def divisions(n, divisor): 3 | if n==divisor: return 1 4 | num = 0; sum = n; 5 | while(sum > divisor-1 and sum != 0): 6 | num += 1 7 | sum = round(math.floor(sum/divisor)) 8 | return num -------------------------------------------------------------------------------- /Python/Beta/Easy.py: -------------------------------------------------------------------------------- 1 | # Make a function that returns the value multiplied by 50 and increased by 6. 2 | # If the value entered is a string it should return "Error". 3 | 4 | # My Solution 5 | def problem(a): 6 | if type(a) == str: return "Error" 7 | return a * 50 + 6 8 | -------------------------------------------------------------------------------- /Python/Beta/Luck.py: -------------------------------------------------------------------------------- 1 | def filter_lucky(lst): 2 | result = [] 3 | for l in lst: 4 | if "7" in str(l): result.append(l) 5 | return result -------------------------------------------------------------------------------- /Python/Beta/WhoAmI.py: -------------------------------------------------------------------------------- 1 | # Write a function that returns the type of its input. 2 | # 3 | # //example 4 | # console.log(checkType("aaa")); 5 | # 6 | # //console : string 7 | 8 | # My Solution 9 | function checkType(value) { 10 | return typeof value; 11 | } 12 | -------------------------------------------------------------------------------- /Ruby/3KYU/HardBomb.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | A bomb has been set to go off! You have to find the wire and cut it in order to 4 | stop the timer. There is a global var that holds the numeric ID to which wire to 5 | cut. Find that and then you can Bomb.CutTheWire(wireKey); 6 | 7 | =end 8 | 9 | # My Solution 10 | p global_variables # Display all global variables 11 | $wireCode = $boom6 12 | bomb.cut_the_wire($wireCode); 13 | 14 | # Better Solution 15 | wire_var = global_variables.grep(/boom/).first 16 | wire_code = eval("#{wire_var}") 17 | bomb.cut_the_wire(wire_code) 18 | 19 | # Another Solution 20 | $wireCode = false # Find the wire. 21 | bomb.cut_the_wire(bomb.instance_variable_get(:@bomb_code)); 22 | -------------------------------------------------------------------------------- /Ruby/5KYU/Anagram.rb: -------------------------------------------------------------------------------- 1 | def anagrams(word, words) 2 | result = [] 3 | final = [] 4 | back = "" 5 | words.each do |x| 6 | back = "" 7 | (x.length-1).downto(0) do |y| 8 | back += x[y] 9 | end 10 | result << back 11 | end 12 | result.each {|x| final << x if word == x} 13 | words.each do |x| 14 | result.each do |y| 15 | final << x if x == y && x.length != 1 16 | end 17 | end 18 | final 19 | end 20 | 21 | 22 | p anagrams('ab', ["ab", "ba", "aa", "bb", "cc", "ac", "bc", "cd"]) 23 | -------------------------------------------------------------------------------- /Ruby/5KYU/BreakCamel.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution so that the function will break up camel casing, using a 4 | space between words. 5 | 6 | Example 7 | 8 | solution('camelCasing') # => should return 'camel Casing' 9 | 10 | =end 11 | 12 | # My Solution 13 | def solution(string) 14 | result = "" 15 | string.split(//).each {|x| x == x.upcase ? result += " " + x : result += x} 16 | result 17 | end 18 | 19 | # Better Solution 20 | def solution(string) 21 | string.gsub /([A-Z])/, ' \1' 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/5KYU/Bulk.rb: -------------------------------------------------------------------------------- 1 | def bulk(arr) 2 | p = 0; c = 0 3 | if arr!=[] 4 | (0..arr.length-1).each do |i| 5 | items = arr[i].split(",") 6 | (0..items.length-1).each do |item| 7 | unit = items[item].split("g ") 8 | units = unit[0].to_f/100 9 | protein = units * $food[unit[1]][0] 10 | carbohydrate = units * $food[unit[1]][1] 11 | fat = units * $food[unit[1]][2] 12 | p += protein 13 | c += (protein * 4) + (carbohydrate * 4) + (fat * 9) 14 | end 15 | end 16 | end 17 | p = p.to_i if p % 1 == 0 18 | c = c.to_i if c % 1 == 0 19 | "Total proteins: #{p} grams, Total calories: #{c}" 20 | end -------------------------------------------------------------------------------- /Ruby/5KYU/CrimboCal.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | It's almost Christmas and you have a pretty cool Christmas Calendar with tasty 4 | sweets and awesome toys in it. As curious as you are, you want to open all the 5 | doors. But unfortunately only the today's door can be opened. Can you find a 6 | way to open the 24th door already? :) 7 | 8 | PS: Yes, if you do this kata on December 24th, you'll get a solved kata for 9 | free. But the other 364 days of the year, you have to trick a bit. 10 | 11 | Good Luck and Merry X-Mas! 12 | 13 | =end 14 | 15 | # My Solution 16 | class Date 17 | def Date.today 18 | return Date.new(2016,12,24) 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/5KYU/DeltaBits.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function convertBits to determine the number of bits required to convert 4 | positive integer A to positive integer B. 5 | 6 | For example, you can change 31 to 14 by flipping the 4th and 0th bit: 7 | 8 | 31 0 0 0 1 1 1 1 1 9 | 14 0 0 0 0 1 1 1 0 10 | --- --------------- 11 | bit 7 6 5 4 3 2 1 0 12 | Thus 31 and 14 should return 2. 13 | 14 | =end 15 | 16 | # My Solution 17 | def convertBits(a,b) 18 | result = 0 19 | a = sprintf '%0100d', a.to_s(2) 20 | b = sprintf '%0100d', b.to_s(2) 21 | a.split("").each_with_index {|x,i| result += 1 if x != b[i]} 22 | result 23 | end 24 | 25 | # Better Solution 26 | def convertBits(a,b) 27 | (a ^ b).to_s(2).count('1') 28 | end 29 | -------------------------------------------------------------------------------- /Ruby/5KYU/Email.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Email addresses are notoriously difficult to validate. 4 | 5 | Create a method that takes an email and returns true if valid, false otherwise. 6 | 7 | Tip: It doesn't have to be perfect. 8 | 9 | For example: 10 | 11 | validate('joe@example.com') => true 12 | validate('joe') => false 13 | Here's tool that may help: http://rubular.com/ 14 | 15 | =end 16 | 17 | # My Soltion 18 | def validate(email) 19 | email =~ /^.*@.*.[.]...$/ ? true : false 20 | end 21 | 22 | # Better Solution 23 | def validate(email) 24 | /^.+@.+\..+$/ === email 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/5KYU/LCM.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that calculates the least common multiple of its arguments; 4 | each argument is assumed to be a non-negative integer. 5 | 6 | =end 7 | 8 | # My Solution 9 | def lcm(*x) 10 | x.reduce(:lcm) 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/5KYU/PigLatin.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Move the first letter of each word to the end of it, then add 'ay' to the end 4 | of the word. 5 | 6 | pig_it('Pig latin is cool') # igPay atinlay siay oolcay 7 | 8 | =end 9 | 10 | # My Solution 11 | def pig_it text 12 | text.split(" ").map {|x| x =~ /[a-zA-Z]/ ? x = x[1..x.length] + x[0] + "ay" : x}.join(" ") 13 | end 14 | 15 | # Better Solution 16 | def pig_it text 17 | text.gsub(/(\w)(\w+)*/, '\2\1ay') 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/5KYU/RegexPass.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You need to write regex that will validate a password to make sure 4 | it meets the follwing criteria: 5 | 6 | At least six characters long 7 | contains a lowercase letter 8 | contains an uppercase letter 9 | contains a number 10 | Valid passwords will only be alphanumeric characters. 11 | 12 | =end 13 | 14 | # My Solution 15 | # 1 or more a-z A-Z Digit 16 | # Only a-zA-Z and Digit 17 | # 6 or more char 18 | regex=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/ 19 | -------------------------------------------------------------------------------- /Ruby/5KYU/SortArray3.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This time the input is an array of course-ids that are formatted in the 4 | following way: 5 | 6 | name-yymm 7 | The return of the function shall first be sorted by yymm, then by the name 8 | (which varies in length). 9 | 10 | =end 11 | 12 | # My Solution 13 | def sortme(courses) 14 | result = [] 15 | final = [] 16 | courses.each do |x| 17 | s,f = x.split("-") 18 | result << [f,s] 19 | end 20 | final = result.sort_by {|a,b| a+b} 21 | result = [] 22 | final.each {|x| result << [x[1]+"-"+x[0]]} 23 | result.flatten 24 | end 25 | 26 | # Better Solution 27 | def sortme(courses) 28 | courses.sort_by{|a| a.split('-').reverse} 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/6KYU/ArrayDiff.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your goal in this kata is to implement an difference function, which subtracts 4 | one list from another. 5 | 6 | It should remove all values from list a, which are present in list b. 7 | 8 | array_diff([1,2],[1]) == [2] 9 | 10 | If a value is present in b, all of its occurrences must be removed from the 11 | other: 12 | 13 | array_diff([1,2],[1]) == [2] 14 | 15 | =end 16 | 17 | # My Solution 18 | def array_diff(a, b) 19 | a - b 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/6KYU/ArrayinArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | We want to extend the array class so that it provides a contains_all? method. 4 | This method will always assume that an array is passed in and will return true 5 | if all values within the passed in array are present within the current array. 6 | 7 | For example: 8 | 9 | items = [1, 2, 3, 4, 5, 6, 7, 8, 9] 10 | 11 | items.contains_all?([1, 2, 3]) # should == true 12 | items.contains_all?([1, 5, 13]) # should == false because 13 is not within the 13 | items array 14 | 15 | =end 16 | 17 | # My Solution 18 | class Array 19 | def contains_all?(other_array) 20 | (self - other_array).length == (self.length - other_array.length) 21 | end 22 | end 23 | 24 | # Better Solution 25 | class Array 26 | def contains_all?(other_array) 27 | (other_array - self).size == 0 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/6KYU/BitCounting.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that takes an (unsigned) integer as input, and returns the 4 | number of bits that are equal to one in the binary representation of that number 5 | 6 | Example: The binary representation of 1234 is 10011010010, so the function 7 | should return 5 in this case 8 | 9 | =end 10 | 11 | # My Solution 12 | def count_bits(n) 13 | result = 0 14 | n.to_s(2).split("").each {|x| result += 1 if x == "1"} 15 | result 16 | end 17 | 18 | # Better Solution 19 | def count_bits(n) 20 | n.to_s(2).count "1" 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/6KYU/Div6.rb: -------------------------------------------------------------------------------- 1 | def is_divisible_by_6(string) 2 | result = [] 3 | ('0'..'9').each do |digit| 4 | number = string.gsub('*', digit).to_i 5 | result << number.to_s if number % 6 == 0 && number.positive? 6 | end 7 | result 8 | end -------------------------------------------------------------------------------- /Ruby/6KYU/FindDivisors.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function named divisors that takes an integer and returns an array with 4 | all of the integer's divisors(except for 1 and the number itself). If the number 5 | is prime return the string '(integer) is prime' (use Either String a in Haskell) 6 | 7 | Example: 8 | 9 | divisors(12); #should return [2,3,4,6] 10 | divisors(25); #should return [5] 11 | divisors(13); #should return "13 is prime" 12 | You can assume that you will only get positive integers as inputs. 13 | 14 | =end 15 | 16 | # My Solution 17 | def divisors(n) 18 | result = [] 19 | 2.upto(n-1) {|x| result << x if n % x == 0} 20 | result == [] ? "#{n} is prime" : result 21 | end 22 | 23 | # Another Solution 24 | def divisors(n) 25 | vals = (2..n/2).select{|x| n%x==0} 26 | vals.empty? ? "#{n} is prime" : vals 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/6KYU/FindLetter.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a method that takes an array of consecutive (increasing) letters 4 | as input and that returns the missing letter in the array. 5 | 6 | You will always get an valid array. And it will be always exactly 7 | one letter be missing. The length of the array will always be at least 2. 8 | The array will always contain letters in only one case. 9 | 10 | Example: 11 | 12 | ['a','b','c','d','f'] -> 'e' 13 | ['O','Q','R','S'] -> 'P' 14 | 15 | =end 16 | 17 | # My Solution 18 | def find_missing_letter(arr) 19 | (arr.length-1).times {|num| return (arr[num].ord+1).chr if arr[num].ord+1 != arr[num+1].ord} 20 | return nil 21 | end 22 | 23 | # Other Solution 24 | def find_missing_letter(arr) 25 | arr[0...-1].each_with_index { |val, ind| return val.next if val.next != arr[ind+1] } 26 | return nil 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/6KYU/FindOddInt.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given an array, find the int that appears an odd number of times. 4 | 5 | There will always be only one integer that appears an odd number of times. 6 | 7 | =end 8 | 9 | # My Solution 10 | def find_it(seq) 11 | new_hash = Hash.new {|data,key| data[key] = 0} 12 | 13 | seq.each {|num| new_hash[num] += 1} 14 | 15 | new_hash.each {|data, key| return data if key.odd?} 16 | end 17 | 18 | # Better Solution 19 | def find_it(seq) 20 | seq.detect { |n| seq.count(n).odd? } 21 | end 22 | 23 | # Another Solution 24 | def find_it(seq) 25 | seq.reduce(:^) 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/6KYU/GA-DE-RY-PO-LU-KI2.rb: -------------------------------------------------------------------------------- 1 | def encode(str, key) ende(str, key) end 2 | def decode(str, key) ende(str, key) end 3 | 4 | def ende(str, key) 5 | key+=key.upcase 6 | rkey = "" 7 | (0..key.length-1).step(2).each {|l| rkey+=key[l+1]+key[l]} 8 | str.tr(key,rkey) 9 | end -------------------------------------------------------------------------------- /Ruby/6KYU/GA-DE-RY-PO-LU-KI3.rb: -------------------------------------------------------------------------------- 1 | def find_the_key(messages, secrets) 2 | code = [] 3 | (0...messages.length).each do |m_index| 4 | (0...messages[m_index].length).each do |l_index| 5 | m_letter = messages[m_index][l_index] 6 | s_letter = secrets[m_index][l_index] 7 | if(m_letter != " " && (m_letter!=s_letter)) 8 | code << [m_letter,s_letter].sort 9 | end 10 | end 11 | end 12 | code.uniq!.sort.flatten.join 13 | end -------------------------------------------------------------------------------- /Ruby/6KYU/Grab.rb: -------------------------------------------------------------------------------- 1 | def grabscrab anagram, dictionary 2 | dictionary.select{|w| anagram.chars.sort == w.chars.sort} 3 | end -------------------------------------------------------------------------------- /Ruby/6KYU/Guess.rb: -------------------------------------------------------------------------------- 1 | class Guesser 2 | def get_number 3 | bot = 1; top = 1000 4 | (0..9).each do |x| 5 | num = ((top-bot)/2).round + bot 6 | a = guess(num); 7 | bot = num if a=="Too low!" 8 | top = num if a=="Too high!" 9 | return num if a=="Correct!" 10 | end 11 | return "Nope!" 12 | end 13 | end -------------------------------------------------------------------------------- /Ruby/6KYU/LongestPalindrome.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | 4 | 5 | =end 6 | 7 | # My Solution 8 | def longest_palindrome s 9 | result = 0 10 | 0.upto(s.length-1) do |x| 11 | 0.upto(s.length-x-1)do |y| 12 | if s[y..y+x] == s[y..y+x].reverse 13 | result = x + 1 if x + 1 > result 14 | end 15 | end 16 | end 17 | result 18 | end 19 | 20 | # Better Solution 21 | def longest_palindrome s 22 | s.length.downto(1) do |n| 23 | s.chars.each_cons(n) do |arr| 24 | return n if arr == arr.reverse 25 | end 26 | end 27 | 0 28 | end 29 | -------------------------------------------------------------------------------- /Ruby/6KYU/Luck.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The test fixture I use for this kata is pre-populated. 4 | 5 | It will compare your guess to a random number generated in Ruby by: 6 | 7 | (Kernel::rand() * 100 + 1).floor 8 | In Javascript/CoffeeScript by: 9 | 10 | Math.floor(Math.random() * 100 + 1) 11 | In Python by: 12 | 13 | randint(1,100) 14 | You can pass by relying on luck or skill but try not to rely on luck. 15 | 16 | "The power to define the situation is the ultimate power." - Jerry Rubin 17 | 18 | Good luck! 19 | 20 | =end 21 | 22 | # My Solution 23 | module Kernel 24 | def self.rand(max=0) 25 | return 0 26 | end 27 | end 28 | 29 | guess = 1 30 | 31 | # Better Solution 32 | srand(1) 33 | guess = 42 34 | 35 | # Another Solution 36 | guess = 37 | -------------------------------------------------------------------------------- /Ruby/6KYU/MasterFiles.rb: -------------------------------------------------------------------------------- 1 | class String 2 | 3 | def is_audio 4 | self.each_char {|x| return false if x == " "} 5 | file = self[0..self.index(".")] 6 | return false if file =~ /\d/ 7 | 4.times do |x| 8 | return true if %w(.mp3 .flac .alac .aac)[x] == self[self.index(".")..-1] 9 | end 10 | false 11 | end 12 | 13 | def is_img 14 | self.each_char {|x| return false if x == " "} 15 | file = self[0..self.index(".")] 16 | return false if file.match(/\A[:alpha:]+\z/) 17 | 5.times do |x| 18 | return true if %w(.jpg .jpeg .png .bmp .gif)[x] == self[self.index(".")..-1] 19 | end 20 | false 21 | end 22 | end 23 | 24 | puts "icon.jpg".is_img 25 | -------------------------------------------------------------------------------- /Ruby/6KYU/Multiples3and5.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | If we list all the natural numbers below 10 that are multiples of 3 or 5, 4 | we get 3, 5, 6 and 9. The sum of these multiples is 23. 5 | 6 | Finish the solution so that it returns the sum of all the multiples of 3 or 5 7 | below the number passed in. 8 | 9 | Note: If the number is a multiple of both 3 and 5, only count it once. 10 | 11 | =end 12 | 13 | # My Solution 14 | def solution(number) 15 | results = 0 16 | 1.upto(number-1) do |x| 17 | results += x if x % 3 == 0 || x % 5 == 0 18 | end 19 | results 20 | end 21 | 22 | # Better Solution 23 | def solution(number) 24 | (1...number).select {|i| i%3==0 || i%5==0}.inject(:+) 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/6KYU/NumBig.rb: -------------------------------------------------------------------------------- 1 | def narcissistic?(value) 2 | result = 0 3 | value.to_s.split("").each {|x| result += x.to_i**value.to_s.length} 4 | result == value 5 | end -------------------------------------------------------------------------------- /Ruby/6KYU/Parent.rb: -------------------------------------------------------------------------------- 1 | def find_children(dancing_brigade) 2 | dancing_brigade.chars.sort_by { |char| [char.downcase, char] }.join 3 | end -------------------------------------------------------------------------------- /Ruby/6KYU/RemoveURL.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the function/method so that it returns the url with anything 4 | after the anchor (#) removed. 5 | 6 | Examples: 7 | 8 | # returns 'www.codewars.com' 9 | remove_url_anchor('www.codewars.com#about') 10 | 11 | # returns 'www.codewars.com?page=1' 12 | remove_url_anchor('www.codewars.com?page=1') 13 | 14 | =end 15 | 16 | # My Solution 17 | def remove_url_anchor(url) 18 | removesub = url[/[^#]+/] 19 | end 20 | 21 | # Another Solution 22 | def remove_url_anchor(url) 23 | url.split('#').first 24 | end 25 | 26 | # Another Solution 27 | def remove_url_anchor(url) 28 | url.split("#").shift 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/6KYU/ReverseWords.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution so that it reverses all of the words within the string 4 | passed in. 5 | 6 | Example: 7 | 8 | solution("The greatest victory is that which requires no battle") 9 | # should return "battle no requires which that is victory greatest The" 10 | 11 | =end 12 | 13 | # My Solution 14 | def solution(sentence) 15 | sentence.split(" ").reverse.join(" ") 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/6KYU/SortArrays.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Simple sort, but this time sort regardless of upper / lower case. 4 | 5 | So the input of 6 | 7 | [ "Hello", "there", "I'm", "fine"] 8 | is translated to 9 | 10 | ["fine", "Hello", "I'm", "there" ] 11 | 12 | =end 13 | 14 | # My Solution 15 | def sortme(names) 16 | result = [] 17 | sorted = names.map.each {|n| n.upcase }.sort 18 | sorted.map.each do |s| 19 | names.each do |n| 20 | result << n if s == n.upcase 21 | end 22 | end 23 | result 24 | end 25 | 26 | # Better Solution 27 | def sortme( names ) 28 | names.sort_by(&:downcase) 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/6KYU/SplitString.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution so that it splits the string into pairs of two characters. 4 | If the string contains an odd number of characters then it should replace the 5 | missing second character of the final pair with an underscore ('_'). 6 | 7 | Examples: 8 | 9 | solution('abc') # should return ['ab', 'c_'] 10 | solution('abcdef') # should return ['ab', 'cd', 'ef'] 11 | 12 | =end 13 | 14 | # My Solution 15 | def solution(str) 16 | result = [] 17 | str += "_" if str.length.odd? 18 | (0..str.length-1).step(2).each {|i| result << str[i]+str[i+1]} 19 | result 20 | end 21 | 22 | # Better Solution 23 | def solution str 24 | (str + '_').scan /../ 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/6KYU/Treasure.rb: -------------------------------------------------------------------------------- 1 | def unlock(str) 2 | str.downcase.tr("abcdefghijklmnopqrstuvwxyz","22233344455566677778889999") 3 | end -------------------------------------------------------------------------------- /Ruby/6KYU/TwistSub.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | Find the sum of the digits of all the numbers from 1 to N (both ends included). 3 | 4 | For N = 10 the sum is 1+2+3+4+5+6+7+8+9+(1+0) = 46 5 | 6 | For N = 11 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) = 48 7 | 8 | For N = 12 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) +(1+2)= 51 9 | 10 | =end 11 | 12 | # My Solution 13 | def solution(n) 14 | string = "" 15 | answer = 0 16 | 1.upto(n) {|x| string += x.to_s} 17 | string.split("").each {|x| answer += x.to_i} 18 | answer 19 | end 20 | 21 | # Better Solution 22 | def solution(n) 23 | (1..n).to_a.join.chars.map(&:to_i).reduce :+ 24 | end 25 | -------------------------------------------------------------------------------- /Ruby/6KYU/ValidPhone.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that accepts a string, and returns true if it is in the form 4 | of a phone number. 5 | 6 | Assume that any integer from 0-9 in any of the spots will produce a valid 7 | phone number. 8 | 9 | Only worry about the following format: 10 | (123) 456-7890 (don't forget the space after the close parentheses) 11 | 12 | Examples: 13 | 14 | validPhoneNumber("(123) 456-7890") => returns true 15 | validPhoneNumber("(1111)555 2345") => returns false 16 | validPhoneNumber("(098) 123 4567") => returns false 17 | 18 | =end 19 | 20 | # My Solution 21 | def validPhoneNumber(phoneNumber) 22 | phoneNumber =~ /^[(]\d{3}[)] \d{3}-\d{4}$/ ? true : false 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/7KYU/Ages.rb: -------------------------------------------------------------------------------- 1 | def two_oldest_ages(ages) 2 | ages.sort[-2..-1] 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/ArrayIndex.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution. It should try to retrieve the value of the array at the 4 | index provided. If the index is out of the array's max bounds then it should 5 | return the default value instead. 6 | 7 | Example: 8 | 9 | data = ['a', 'b', 'c'] 10 | solution(data, 1, 'd') # should == 'b' 11 | solution(data, 5, 'd') # should == 'd' 12 | 13 | # negative values work as long as they aren't out of the length bounds 14 | solution(data, -1, 'd') # should == 'c' 15 | solution(data, -5, 'd') # should == 'd' 16 | 17 | =end 18 | 19 | # My Solution 20 | def solution(items, index, default_value) 21 | index.abs > items.length ? default_value : items[index] 22 | end 23 | 24 | # Another Solution 25 | def solution(items, index, default_value) 26 | items.fetch(index, default_value) 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/7KYU/AverageScores.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function that returns the average of an array of numbers ("scores"), 4 | rounded to the nearest whole number. You are not allowed to use any loops 5 | (including for, for/in, while, and do/while loops). 6 | 7 | =end 8 | 9 | # My Solution 10 | def average(array) 11 | (array.inject(:+).to_f / array.length).round 12 | end 13 | 14 | # Another Solution 15 | def average(array) 16 | array.inject(:+).fdiv(array.length).round 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/Box.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a number as a parameter, return an array containing strings which form a box. 4 | Like this: 5 | 6 | n = 5 7 | 8 | [ 9 | '-----', 10 | '- -', 11 | '- -', 12 | '- -', 13 | '-----' 14 | ] 15 | n = 3 16 | 17 | [ 18 | '---', 19 | '- -', 20 | '---' 21 | ] 22 | 23 | =end 24 | 25 | # My Solution 26 | def box(n) 27 | result = [] 28 | result << "-" * n 29 | (2..n-1).each {|x| result << "-" + (" " * (n-2)) + "-" } 30 | result << "-" * n 31 | end 32 | -------------------------------------------------------------------------------- /Ruby/7KYU/BumpRoad.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your car is old, it breaks easily. The shock absorbers are gone and 4 | you think it can handle about 15 more bumps before it dies totally. 5 | 6 | Unfortunately for you, your drive is very bumpy! Given a string 7 | showing either flat road ("_") or bumps ("n"), work out if you 8 | make it home safely. 15 bumps or under, return "Woohoo!", 9 | over 15 bumps return "Car Dead". 10 | 11 | =end 12 | 13 | # My Solution 14 | def bump(x) 15 | x.count("n").to_i > 15 ? "Car Dead" : "Woohoo!" 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/7KYU/CaseSwap.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a string, swap the case for each of the letters. 4 | 5 | e.g. CodEwArs --> cODeWaRS 6 | 7 | =end 8 | 9 | # My solution - Long but it works :-) 10 | def swap(string) 11 | newstr = "" 12 | string.each_char do |letter| 13 | if letter[0].ord >= 65 && letter[0].ord <= 90 14 | newstr += (letter[0].ord+32).chr 15 | elsif letter[0].ord >= 97 && letter[0].ord <= 122 16 | newstr += (letter[0].ord-32).chr 17 | else 18 | newstr += letter 19 | end 20 | end 21 | newstr 22 | end 23 | 24 | # Better solution 25 | def swap(string) 26 | string.swapcase 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/7KYU/CloseTen.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a number return the closest number to it that is divisible by 10. 4 | 5 | Example input: 6 | 7 | 22 8 | 25 9 | 37 10 | Expected output: 11 | 12 | 20 13 | 30 14 | 40 15 | 16 | =end 17 | 18 | # My Solution 19 | def closest_multiple_10(i) 20 | i.round(-1) 21 | end 22 | 23 | # Other Solution 24 | def closest_multiple_10(i) 25 | (i/10.0).round * 10 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/Count.rb: -------------------------------------------------------------------------------- 1 | def most_frequent_item_count(c) 2 | c.count(c.max_by{|x| c.count(x)}) 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/CountChars.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The goal of this kata is to write a function that takes two inputs: a string and 4 | a character. The function will count the number of times that character appears 5 | in the string. The count is case insensitive. 6 | 7 | For example: 8 | 9 | count_char("fizzbuzz","z") => 4 10 | count_char("Fancy fifth fly aloof","f") => 5 11 | The character can be any alphanumeric character. 12 | 13 | Good luck. 14 | 15 | =end 16 | 17 | # My Solution 18 | def count_char(string, char) 19 | string.upcase.count(char.upcase) 20 | end 21 | 22 | # Better Solution 23 | def count_char(string, char) 24 | string.scan(/#{char}/i).size 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/7KYU/CountLetandDig.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Bob is a lazy man. 4 | 5 | He needs you to create a method that can determine how many letters and digits 6 | are in a given string. 7 | 8 | example: 9 | 10 | "hel2!lo" --> 6 11 | 12 | "wicked .. !" --> 6 13 | 14 | "!?..A" --> 1 15 | 16 | Complete the method: 17 | 18 | Ruby: count_letters_and_digits(input) 19 | 20 | =end 21 | 22 | # My Solution 23 | def count_letters_and_digits(input) 24 | result = 0 25 | input.split("").each {|x| result +=1 if x =~ /\d/ || x =~ /[a-zA-Z]/} 26 | result 27 | end 28 | 29 | # Better Solution 30 | def count_letters_and_digits(input) 31 | input.scan(/[a-zA-Z0-9]/).length 32 | end 33 | 34 | # Another Solution 35 | def count_letters_and_digits(input) 36 | puts input 37 | input.scan(/[^\W_]/).join.size 38 | end 39 | -------------------------------------------------------------------------------- /Ruby/7KYU/CountMarksQuestion.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Count the number of exclamation marks and question marks, return the product. 4 | The string only contains ! and ?. 5 | Examples 6 | 7 | product("") === 0 8 | product("!") === 0 9 | product("!!") === 0 10 | product("!??") === 2 11 | product("!???") === 3 12 | product("!!!??") === 6 13 | product("!!!???") === 9 14 | product("!???!!") === 9 15 | product("!????!!!?") === 20 16 | 17 | =end 18 | 19 | # My Solution 20 | def product(s) 21 | s.count("!") * s.count("?") 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/7KYU/Covfefe.rb: -------------------------------------------------------------------------------- 1 | def covfefe(tweet) 2 | tweet.include?("coverage") ? tweet.gsub("coverage","covfefe") : tweet +" covfefe" 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/DNA.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and 4 | carries the "instructions" for the development and functioning of living 5 | organisms. 6 | 7 | If you want to know more http://en.wikipedia.org/wiki/DNA 8 | 9 | In DNA strings, symbols "A" and "T" are complements of each other, 10 | as "C" and "G". You have function with one side of the DNA; you need to get the 11 | other complementary side. DNA strand is never empty or there is no DNA at all 12 | (again, except for Haskell). 13 | 14 | DNA_strand ("ATTGC") # return "TAACG" 15 | 16 | DNA_strand ("GTAT") # return "CATA" 17 | 18 | =end 19 | 20 | # My Solution 21 | def DNA_strand(dna) 22 | dna.tr("ATCG","TAGC") 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/7KYU/DecOrder.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your task is to make a function that can take any non-negative 4 | integer as a argument and return it with it's digits in 5 | descending order. Descending order means that you take the 6 | highest digit and place the next highest digit immediately 7 | after it. 8 | 9 | Examples: 10 | 11 | Input: 145263 Output: 654321 12 | 13 | Input: 1254859723 Output: 9875543221 14 | 15 | =end 16 | 17 | # My Solution 18 | def descending_order(n) 19 | n.to_s.split("").sort!.reverse!.join.to_i 20 | end 21 | 22 | # Another Solution 23 | def descending_order(n) 24 | n.to_s.chars.sort.reverse.join.to_i 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/7KYU/DivideConquar.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a mixed array of number and string representations of integers, add up 4 | the string integers and subtract this from the total of the non-string integers. 5 | 6 | Return as a number. 7 | 8 | =end 9 | 10 | # My Solution 11 | def div_con(x) 12 | pos = 0 ; neg = 0 13 | x.each {|n| (n.is_a? Integer) ? (pos += n) : (neg += n.to_i)} 14 | pos - neg 15 | end 16 | 17 | # Better Solution 18 | def div_con(x) 19 | x.reduce(0) { |s, i| i.is_a?(String) ? s - i.to_i : s + i } 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/7KYU/Elements.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given two arrays of integers m and n, test if they contain at least one 4 | identical element. Return true if they do; false if not. 5 | 6 | Your code must handle any value within the range of a 32-bit integer, 7 | and must be capable of handling either array being empty (which is a false 8 | result, as there are no duplicated elements). 9 | 10 | =end 11 | 12 | # My Solution 13 | def duplicate_elements(m, n) 14 | (m - n).length < m.length ? true : false 15 | end 16 | 17 | # Another Solution 18 | def duplicate_elements(m, n) 19 | (m & n).any? 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/7KYU/Exclamation.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Move all exclamation marks to the end of the sentence 4 | Examples 5 | 6 | remove("Hi!") === "Hi!" 7 | remove("Hi! Hi!") === "Hi Hi!!" 8 | remove("Hi! Hi! Hi!") === "Hi Hi Hi!!!" 9 | remove("Hi! !Hi Hi!") === "Hi Hi Hi!!!" 10 | remove("Hi! Hi!! Hi!") === "Hi Hi Hi!!!!" 11 | 12 | =end 13 | # My Solution 14 | def remove(s) 15 | n = s.count("!") 16 | s = s.gsub("!","") + ("!" * n) 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/FeqSeq.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Return an output string that translates an input string s/$s by replacing each 4 | character in s/$s with a number representing the number of times that character 5 | occurs in s/$s and separating each number with the character(s) sep/$sep. 6 | 7 | =end 8 | 9 | # My Solution 10 | def freqSeq(str, seq) 11 | str.split("").each.map {|x| str.count(x)}.join(seq) 12 | end 13 | 14 | # Another Solution 15 | def freqSeq(str, seq) 16 | str.chars.map {|x| str.count(x)}.join(seq) 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/FindVowels.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | We want to know the index of the vowels in a given word, for example, there are 4 | two vowels in the word super (the second and fourth letters). 5 | 6 | So given a string "super", we should return a list of [2, 4]. 7 | 8 | Some examples: 9 | Mmmm => [] 10 | Super => [2,4] 11 | Apple => [1,5] 12 | YoMama -> [1,2,4,6] 13 | NOTE: Vowels in this context refers to English Language Vowels - a e i o u y 14 | 15 | NOTE: this is indexed from [1..n] (not zero indexed!) 16 | 17 | =end 18 | 19 | # My Solution 20 | def vowel_indices(word) 21 | result = [] 22 | word.split("").each_with_index {|x,i| result << i+1 if x.downcase =~ /[aeiouy]/} 23 | result 24 | end 25 | 26 | # Better Solution 27 | def vowel_indices(word) 28 | word.chars.map.with_index(1) {|c, i| i if /[aeiouy]/i =~ c }.compact 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/7KYU/Flatten.rb: -------------------------------------------------------------------------------- 1 | def flatten_and_sort(array) 2 | array.flatten.sort 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Flatten2.rb: -------------------------------------------------------------------------------- 1 | def flatten(array) 2 | array.flatten 1 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/FloatPrec.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Update the solution method to round the argument value to the closest precision 4 | of two. The argument will always be a float. 5 | 6 | solution(23.23456) # should == 23.23 7 | solution(1.546) # should == 1.55 8 | 9 | =end 10 | 11 | # My Solution 12 | def solution(value) 13 | value.round(2) 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/7KYU/Friday.rb: -------------------------------------------------------------------------------- 1 | def killcount(counselors, jason) 2 | counselors.select{|x| x[1] < jason}.map{|x| x[0]} 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/FriendorFoe.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Make a program that filters a list of strings and returns a 4 | list with only your friends name in it. 5 | 6 | If a name has 4 letters in it, you can be sure that it has 7 | to be a friend of yours! 8 | 9 | Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"] 10 | Output = ["Ryan", "Yous"] 11 | 12 | =end 13 | 14 | # My Solution 15 | def friend(friends) 16 | friends.select { |name| name.length == 4 } 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/Fruit.rb: -------------------------------------------------------------------------------- 1 | def calculate(string) 2 | string = string.split(" ") 3 | string[-2] == "loses" ? string[2].to_i - string[-1].to_i : string[2].to_i + string[-1].to_i 4 | end -------------------------------------------------------------------------------- /Ruby/7KYU/GA-DE-RY-PO-LU-KI .rb: -------------------------------------------------------------------------------- 1 | def encode(str) ende(str) end 2 | def decode(str) ende(str) end 3 | def ende(str) str.tr("gaderypolukiGADERYPOLUKI","agedyropulikAGEDYROPULIK") end -------------------------------------------------------------------------------- /Ruby/7KYU/GradPara.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This kata is all about adding numbers. 4 | 5 | You will create a function named add. This function will return the 6 | sum of all the arguments. Sounds easy, doesn't it?? 7 | 8 | Well here's the twist. The inputs will gradually increase with 9 | their index as parameter to the function. 10 | 11 | add(3,4,5) #returns (3*1)+(4*2)+(5*3)=26 12 | Remember the function will return 0 if no arguments are passed. 13 | 14 | Example 15 | 16 | add() #=> 0 17 | add(1,2,3) #=> 14 18 | add(1,4,-5,5) #=> 14 19 | 20 | =end 21 | 22 | # My Solution 23 | def add(*args) 24 | @x = 0 25 | args.each_with_index {|num,index| @x += num * (index+1)} 26 | return @x 27 | end 28 | 29 | # Better Solution 30 | def add(*args) 31 | args.map.with_index(1){|a,i| a*i}.reduce(:+) || 0 32 | end 33 | -------------------------------------------------------------------------------- /Ruby/7KYU/HeadTail.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your job is to implement these functions in your given language. Make sure it 4 | doesn't edit the array; that would cause problems! Here's a cheat sheet: 5 | 6 | | HEAD | <----------- TAIL ------------> | 7 | [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 8 | | <----------- INIT ------------> | LAST | 9 | 10 | head [x] = x 11 | tail [x] = [] 12 | init [x] = [] 13 | last [x] = x 14 | 15 | =end 16 | 17 | # My Solution 18 | def head(arr) 19 | arr[0] 20 | end 21 | 22 | def tail(arr) 23 | arr[1..-1] 24 | end 25 | 26 | def init(arr) 27 | arr[0..-2] 28 | end 29 | 30 | def last(arr) 31 | arr[-1] 32 | end 33 | -------------------------------------------------------------------------------- /Ruby/7KYU/Hit.rb: -------------------------------------------------------------------------------- 1 | def counter_effect(hit_count) 2 | result = [] 3 | hit_count.split("").each{|i| result << (0..i.to_i).map{|a|a}} 4 | result 5 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Ifs.rb: -------------------------------------------------------------------------------- 1 | def no_ifs_no_buts(a, b) "#{a} is #{['smaller than', 'equal to', 'greater than'][(a <=> b) + 1]} #{b}" end -------------------------------------------------------------------------------- /Ruby/7KYU/Incrementer.rb: -------------------------------------------------------------------------------- 1 | def incrementer(num) 2 | num.each_with_index.map { |x,i| (x+(i+1)).to_s[-1].to_i} 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/IsEven.rb: -------------------------------------------------------------------------------- 1 | def is_even(n) n & 1 == 0 end -------------------------------------------------------------------------------- /Ruby/7KYU/Isograms.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | An isogram is a word that has no repeating letters, consecutive or 4 | non-consecutive. Implement a function that determines whether a string that 5 | contains only letters is an isogram. Assume the empty string is an isogram. 6 | Ignore letter case. 7 | 8 | is_isogram("Dermatoglyphics" ) == true 9 | is_isogram("aba" ) == false 10 | is_isogram("moOse" ) == false # -- ignore letter case 11 | 12 | =end 13 | 14 | # My Solution 15 | def is_isogram(string) 16 | string.split("").each {|x| return false if string.downcase.count(x.downcase) > 1} 17 | true 18 | end 19 | 20 | # Better Solution 21 | def is_isogram(string) 22 | string.downcase.chars.uniq == string.downcase.chars 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/7KYU/JadenString.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Jaden Smith, the son of Will Smith, is the star of films such 4 | as The Karate Kid (2010) and After Earth (2013). 5 | Jaden is also known for some of his philosophy that he 6 | delivers via Twitter. When writing on Twitter, he is 7 | known for almost always capitalizing every word. 8 | 9 | Your task is to convert strings to how they would 10 | be written by Jaden Smith. The strings are actual 11 | quotes from Jaden Smith, but they are not capitalized 12 | in the same way he originally typed them. 13 | 14 | Example: 15 | 16 | Not Jaden-Cased: "How can mirrors be real if our eyes aren't real" 17 | Jaden-Cased: "How Can Mirrors Be Real If Our Eyes Aren't Real" 18 | 19 | =end 20 | 21 | # My Solution 22 | class String 23 | def toJadenCase 24 | self.split.map(&:capitalize).join(" ") 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/ListFiltering.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | In this kata you will create a function that takes a list of non-negative 4 | integers and strings and returns a new list with the strings filtered out. 5 | 6 | Example 7 | 8 | filter_list([1,2,'a','b']) == [1,2] 9 | filter_list([1,'a','b',0,15]) == [1,0,15] 10 | filter_list([1,2,'aasf','1','123',123]) == [1,2,123] 11 | 12 | =end 13 | 14 | # My Solution 15 | def filter_list(l) 16 | result = [] 17 | l.map {|x| result << x.to_i if x.is_a? Integer} 18 | result 19 | end 20 | 21 | # Better Solution 22 | def filter_list(l) 23 | l.reject { |x| x.is_a? String } 24 | end 25 | 26 | # Another Solution 27 | def filter_list(l) 28 | l.select{|i| i.is_a?(Integer)} 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/7KYU/LuckyNum.rb: -------------------------------------------------------------------------------- 1 | def is_lucky(n) 2 | n % 9 == 0 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Marks.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Remove all exclamation marks from sentence except at the end. 4 | 5 | Examples 6 | 7 | remove("Hi!") == "Hi!" 8 | remove("Hi!!!") == "Hi!!!" 9 | remove("!Hi") == "Hi" 10 | remove("!Hi!") == "Hi!" 11 | remove("Hi! Hi!") == "Hi Hi!" 12 | remove("Hi") == "Hi" 13 | 14 | =end 15 | 16 | # My Solution 17 | def remove(s) 18 | r = s.tr("!","") 19 | (s.length-1).downto(0) {|x| s.split("")[x] == "!" ? r+="!" : break} 20 | r 21 | end 22 | 23 | # Better Solution 24 | def remove(s) 25 | s.gsub(/!+(\w|\s)/, '\1') 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/Median.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The mean (or average) is the most popular measure of central tendency; 4 | however it does not behave very well when the data is skewed 5 | (i.e. wages distribution). In such cases, it's better to use the median. 6 | 7 | Your task for this kata is to find the median of an array consisting 8 | of n elements. 9 | 10 | You can assume that all inputs are arrays of numbers in integer format. 11 | For the empty array your code should return NaN 12 | (false in JavaScript/NULL in PHP). 13 | 14 | Examples: 15 | 16 | Input [1, 2, 3, 4] --> Median 2.5 17 | 18 | Input [3, 4, 1, 2, 5] --> Median 3 19 | 20 | =end 21 | 22 | # My Solution 23 | def median(arr) 24 | mid = arr.length / 2 25 | sorted = arr.sort 26 | arr.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid-1]) 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/7KYU/Mumbling.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This time no story, no theory. The examples below show you how to 4 | write function accum: 5 | 6 | Examples: 7 | 8 | accum("abcd") # "A-Bb-Ccc-Dddd" 9 | accum("RqaEzty") # "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy" 10 | accum("cwAt") # "C-Ww-Aaa-Tttt" 11 | The parameter of accum is a string which includes only letters 12 | from a..z and A..Z. 13 | 14 | =end 15 | 16 | # My Solution 17 | def accum(s) 18 | word = "" 19 | s.split("").to_a.each_with_index {|letter,index| word += (letter * (index+1) + "-").capitalize} 20 | word[0..-2] 21 | end 22 | 23 | # Better Solution 24 | def accum(s) 25 | s.chars.each_with_index.map{ |c, i| c.upcase + c.downcase * i }.join('-') 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/Near5.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given an array of numbers, return an array, with each member of input 4 | array rounded to a nearest number, divisible by 5. 5 | 6 | For example: 7 | 8 | roundToFive([34.5, 56.2, 11, 13]); 9 | should return 10 | 11 | [35, 55, 10, 15] 12 | 13 | =end 14 | 15 | # My Solution 16 | def round_to_five(numbers) 17 | numbers.map {|x| (x / 5.0).round * 5} 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/7KYU/Oddities.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a small function that returns the values of an array that are not odd. 4 | 5 | All values in the array will be integers. Return the good values in the order 6 | they are given. 7 | 8 | def no_odds( values ) 9 | 10 | =end 11 | 12 | # My Solution 13 | def no_odds( values ) 14 | values.reject {|x| x.odd?} 15 | end 16 | 17 | # Another Solution 18 | def no_odds( values ) 19 | values.select &:even? 20 | end 21 | 22 | # Another Solution 23 | def no_odds( values ) 24 | values.reject(&:odd?) 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/7KYU/Office1.rb: -------------------------------------------------------------------------------- 1 | def outed(meet, boss) 2 | result = 0 3 | meet.each {|person,rate| result += person.to_s==boss ? 2 * rate : rate } 4 | result/meet.length <= 5 ? "Get Out Now!" : "Nice Work Champ!" 5 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Office2.rb: -------------------------------------------------------------------------------- 1 | def boredom(staff) 2 | scores = {'accounts'=>1,'finance'=>2, 'canteen'=>10, 'regulation'=>3, 'trading'=>6, 'change'=>6, 'IS'=>8, 'retail'=>5, 'cleaning'=>4, 'pissing about'=>25} 3 | total = 0 4 | staff.each {|name, dept| total += scores[dept]} 5 | total <= 80 ? "kill me now" : total < 100 ? "i can handle this" : "party time!!" 6 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Office3.rb: -------------------------------------------------------------------------------- 1 | def broken(x) 2 | x.split("").map{|i| i == "1" ? i = "0" : i = "1"}.join 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/Order.rb: -------------------------------------------------------------------------------- 1 | def order_word(s) 2 | s == '' || s == nil ? "Invalid String!" : s.split('').sort.join 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/PadNum.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution so that it returns a formatted string. 4 | The return value should equal "Value is VALUE" where value is 5 | a 5 digit padded number. 6 | 7 | Example: 8 | 9 | solution(5) # should return "Value is 00005" 10 | 11 | =end 12 | 13 | # My solution 14 | def solution(value) 15 | return "Value is %05d" % value 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/7KYU/Palindrones.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | An array is given with palindromic and non-palindromic numbers. Palindromic 4 | number is a number that is the same from a reveresed order. For example 122 5 | is not a palindromic number but 202 is one. 6 | 7 | Your task is to write a function that returns an array with only 1s and 0s, 8 | where all palindromic number is replaced with a 1 and all non-palindromic 9 | number of the original array replaced with a 0. 10 | 11 | For example: 12 | 13 | [101, 2, 85, 33, 14014] will return [1, 1, 0, 1, 0] 14 | 15 | and [45, 21, 303, 56] will return [0, 0, 1, 0] 16 | 17 | =end 18 | 19 | # My Solution 20 | def convert_palindromes(numbers) 21 | numbers.map {|x| x.to_s == x.to_s.reverse ? 1 : 0} 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/7KYU/RemoveMark.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Remove all exclamation marks from the end of words. Words are separated by spaces in the sentence. 4 | Examples 5 | 6 | remove("Hi!") === "Hi" 7 | remove("Hi!!!") === "Hi" 8 | remove("!Hi") === "!Hi" 9 | remove("!Hi!") === "!Hi" 10 | remove("Hi! Hi!") === "Hi Hi" 11 | remove("!!!Hi !!hi!!! !hi") === "!!!Hi !!hi !hi" 12 | Note 13 | 14 | Please don't post issue about difficulty or duplicate. 15 | 16 | =end 17 | 18 | def remove(s) 19 | result = [] 20 | s.split(" ").map do |x| 21 | until x[-1] != "!" 22 | x = x.chomp("!") 23 | end 24 | result << x 25 | end 26 | result.join(" ") 27 | end 28 | 29 | def remove(s) 30 | s.split.map { |w| w.sub(/!*$/, "") }.join(" ") 31 | end 32 | -------------------------------------------------------------------------------- /Ruby/7KYU/ReverseString.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the solution so that it reverses the string value passed into it. 4 | 5 | solution('world') # returns 'dlrow' 6 | 7 | =end 8 | 9 | # My Solution 10 | def solution(str) 11 | str.reverse 12 | end 13 | -------------------------------------------------------------------------------- /Ruby/7KYU/SayHello.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Say hello! 4 | 5 | Write a function to greet a person. Function will take name as input 6 | and greet the person by saying hello. Return null/nil if input is 7 | empty string or null/nil. 8 | 9 | Example: 10 | 11 | greet("Niks") == "hello Niks!" 12 | greet("") == nil; # Return nil if input is empty string 13 | greet(nil) == nil; # Return nil if input is nil 14 | 15 | =end 16 | 17 | # My Solution 18 | def greet(name) 19 | name == "" || name == nil ? nil : "hello #{name}!" 20 | end 21 | 22 | # Better Solution 23 | def greet(name) 24 | "hello #{name}!" unless name.to_s.empty? 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/7KYU/Scoring.rb: -------------------------------------------------------------------------------- 1 | def score_test(tests, right, omit, wrong) 2 | (tests.count(0) * right) + (tests.count(1) * omit) - (tests.count(2) * wrong) 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/SortChar.rb: -------------------------------------------------------------------------------- 1 | def sortMe(arr) 2 | arr.sort_by {|x| x.to_s[-1]} 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/SplitParts.rb: -------------------------------------------------------------------------------- 1 | def split_in_parts (s, p) 2 | (0...s.length).step(p).map {|w| s[w...w+p]}.join(" ") 3 | end -------------------------------------------------------------------------------- /Ruby/7KYU/SquareDigits.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Welcome. In this kata, you are asked to square every digit of a number. 4 | 5 | For example, if we run 9119 through the function, 811181 will come out. 6 | 7 | Note: The function accepts an integer and returns an integer 8 | 9 | =end 10 | 11 | # My Solution 12 | def square_digits num 13 | num.to_s.split("").map {|x| x.to_i*x.to_i}.join.to_i 14 | end 15 | 16 | # Another Solution 17 | def square_digits num 18 | num.to_s.chars.map{|x| x.to_i**2}.join.to_i 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/7KYU/StarCode.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | All Star Code Challenge #1 4 | 5 | Write a function, called sumPPG, that takes two NBA player objects and sums 6 | their PPG 7 | 8 | Examples: 9 | 10 | iverson = { "team"=>"76ers", "ppg"=> 11.2 } 11 | jordan = { "team"=>"bulls", "ppg"=> 20.2 } 12 | sum_ppg(iverson, jordan) # => 31.4 13 | 14 | =end 15 | 16 | # My Solution 17 | def sum_ppg(player_one, player_two) 18 | player_one["ppg"] + player_two["ppg"] 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/7KYU/StringDrill.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This challenge extends the previous repeater() challenge. Just like last time, 4 | your job is to write a function that accepts a string and a number as arguments. 5 | This time, however, you should format the string you return like this: 6 | 7 | >>> repeater('yo', 3) 8 | '"yo" repeated 3 times is: "yoyoyo"' 9 | >>> repeater('WuB', 6) 10 | '"WuB" repeated 6 times is: "WuBWuBWuBWuBWuBWuB"' 11 | 12 | =end 13 | 14 | # My Solution 15 | def repeater(string, n) 16 | "\"#{string}\" repeated #{n} times is: \"#{string * n}\"" 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/StringQuote.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This function should take two string parameters: a person's name (name) and a 4 | quote of theirs (quote), and return a string attributing the quote to the person 5 | in the following format: 6 | 7 | '[name] said: "[quote]"' 8 | For example, if name is 'Grae' and 'quote' is 'Practice makes perfect' then your 9 | function should return the string 10 | 11 | 'Grae said: "Practice makes perfect"' 12 | Unfortunately, something is wrong with the instructions in the function body. 13 | Your job is to fix it so the function returns correctly formatted quotes. 14 | 15 | Click the "Train" button to get started, and be careful with your quotation 16 | marks. 17 | 18 | =end 19 | 20 | # My Solution 21 | def quotable(name, quote) 22 | "#{name} said: \"#{quote}\"" 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/7KYU/SumDigits.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function named sumDigits which takes a number as input and returns the 4 | sum of the absolute value of each of the number's decimal digits. For example: 5 | 6 | sumDigits 10 # Returns 1 7 | sumDigits 99 # Returns 18 8 | sumDigits -32 # Returns 5 9 | Let's assume that all numbers in the input will be integer values. 10 | 11 | =end 12 | 13 | # My Solution 14 | def sumDigits(number) 15 | num = 0 16 | number.to_s.split("").each {|x| num += x.to_i.abs} 17 | num 18 | end 19 | 20 | # Better Solution 21 | def sumDigits(number) 22 | number.to_s.chars.map(&:to_i).reduce(:+) 23 | end 24 | 25 | # Another Solution 26 | def sumDigits(number) 27 | number.abs.to_s.split("").map(&:to_i).inject(&:+) 28 | end 29 | -------------------------------------------------------------------------------- /Ruby/7KYU/SumLowestTwo.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function that returns the sum of the two lowest positive numbers given 4 | an array of minimum 4 integers. No floats or empty arrays will be passed. 5 | 6 | For example, when an array is passed like [19,5,42,2,77], the output should be 7 7 | 8 | [10,343445353,3453445,3453545353453] should return 3453455. 9 | 10 | =end 11 | 12 | # My Solution 13 | def sum_two_smallest_numbers(numbers) 14 | numbers.sort! 15 | numbers[0] + numbers[1] 16 | end 17 | 18 | # Better Solution 19 | def sum_two_smallest_numbers(numbers) 20 | numbers.min(2).reduce(:+) 21 | end 22 | 23 | # Another Solution 24 | def sum_two_smallest_numbers(numbers) 25 | numbers.sort[0..1].inject(:+) 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/SumSequence.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your task is to make function, which returns the sum of a sequence of integers. 4 | 5 | The sequence is defined by 3 non-negative values: begin, end, step. 6 | 7 | If begin value is greater than the end, function should returns 0 8 | 9 | =end 10 | 11 | # My Solution 12 | def sequence_sum(s, f, step) 13 | return 0 if s > f 14 | (s..f).step(step).each.inject(:+) 15 | end 16 | 17 | # Better Solution 18 | def sequence_sum(begin_number, end_number, step) 19 | (begin_number..end_number).step(step).reduce(0, :+) 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/7KYU/Suzuki.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Suzuki needs help lining up his students! 4 | 5 | Today Suzuki will be interviewing his students to ensure they are progressing in their training. He decided to schedule the interviews based on the length of the students name in descending order. The students will line up and wait for their turn. 6 | 7 | You will be given a string of student names. Sort them and return a list of names in descending order. 8 | 9 | =end 10 | 11 | students = 'Tadashi Takahiro Takao Takashi Takayuki Takehiko Takeo Takeshi Takeshi' 12 | 13 | # Split the string into and array where it find a ' ' 14 | # Sort by length with .sort_by 15 | # Reverse the results with .reverse 16 | 17 | puts students.split(' ').sort_by{|str| str.length}.reverse 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/Switcheroo.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a string made up of letters a, b, and/or c, 4 | switch the position of letters a and b 5 | (change a to b and vice versa). Leave any incidence 6 | of c untouched. 7 | 8 | Example: 9 | 10 | 'acb' --> 'bca' 11 | 'aabacbaa' --> 'bbabcabb' 12 | 13 | =end 14 | 15 | # My Solution 16 | def switcheroo(x) 17 | word = "" 18 | x.split("").each do |l| 19 | if l == 'a' 20 | word += 'b' 21 | elsif l == 'b' 22 | word += 'a' 23 | else 24 | word += l 25 | end 26 | end 27 | word 28 | end 29 | 30 | # Better Solution 31 | def switcheroo(x) 32 | x.tr('ab','ba') 33 | end 34 | -------------------------------------------------------------------------------- /Ruby/7KYU/Time.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Chingel was creating a function which would return true if the input followed 4 | this time format 14-10-2016 01:12 and false otherwise. But looks like he has 5 | messed it up. Could you help him out? Please! 6 | 7 | =end 8 | 9 | # My Solution 10 | def is_it_bugged(code) 11 | code =~ /^[0-3][0-9]-[0-1][0-9]-[0-9]{4} [0-2][0-9]:[0-5][0-9]$/ ? true : false 12 | end 13 | 14 | # Better Solution 15 | def is_it_bugged(code) 16 | return !! (code =~ /^\d\d\-\d\d\-\d\d\d\d\s\d\d\:\d\d$/) 17 | end 18 | 19 | # Another Solution 20 | def is_it_bugged(code) 21 | /\A\d{2}-\d{2}-\d{4} \d{2}:\d{2}\z/ === code 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/7KYU/Triangle.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Triangular numbers are so called because of the equilateral triangular shape 4 | that they occupy when laid out as dots. i.e. 5 | 6 | 1st (1) 2nd (3) 3rd (6) 7 | * ** *** 8 | * ** 9 | * 10 | You need to return the nth triangular number. You should return 0 for out of 11 | range values: 12 | 13 | triangular(0)==0, 14 | triangular(2)==3, 15 | triangular(3)==6, 16 | triangular(-10)==0 17 | 18 | =end 19 | 20 | # My Solution 21 | def triangular( n ) 22 | num = 0 23 | 1.upto(n) {|x| num += x} 24 | num 25 | end 26 | 27 | # Better Solution 28 | def triangular( n ) 29 | n < 0 ? 0 : (n*n+n)/2 30 | end 31 | 32 | # Another Solution 33 | def triangular(n) 34 | (1..n).reduce(0, &:+) 35 | end 36 | -------------------------------------------------------------------------------- /Ruby/7KYU/Trolls.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Trolls are attacking your comment section! 4 | 5 | A common way to deal with this situation is to remove all of the vowels from the 6 | trolls' comments, neutralizing the threat. 7 | 8 | Your task is to write a function that takes a string and return a new string 9 | with all vowels removed. 10 | 11 | For example, the string "This website is for losers LOL!" would become 12 | "Ths wbst s fr lsrs LL!". 13 | 14 | =end 15 | 16 | # My Solution 17 | def disemvowel(str) 18 | str.tr('aeiouAEIOU','') 19 | end 20 | 21 | # Another Solution 22 | def disemvowel(str) 23 | str.delete('aeiouAEIOU') 24 | end 25 | -------------------------------------------------------------------------------- /Ruby/7KYU/TwotoOne.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | a = "xyaabbbccccdefww" 4 | b = "xxxxyyyyabklmopq" 5 | longest(a, b) -> "abcdefklmopqwxy" 6 | 7 | a = "abcdefghijklmnopqrstuvwxyz" 8 | longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" 9 | 10 | =end 11 | 12 | # My Solution 13 | def longest(a1, a2) 14 | (a1+a2).split("").sort.uniq.join("") 15 | end 16 | 17 | # Other Solution 18 | def longest(a1, a2) 19 | (a1+a2).chars.sort.uniq.join 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/7KYU/ValueArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Is every value in the array an array? 4 | 5 | This should only test the second array dimension of the array. 6 | The values of the nested arrays don't have to be arrays. 7 | 8 | Examples: 9 | 10 | [[1],[2]] => true 11 | ['1','2'] => false 12 | [{1:1},{2:2}] => false 13 | 14 | =end 15 | 16 | # My Solution 17 | def arrCheck(value) 18 | @temp = true 19 | value.each {|x| (x.is_a? (Array)) == false ? (@temp = false ; break) : @temp = true } 20 | @temp 21 | end 22 | 23 | # Better Solution 24 | def arrCheck(value) 25 | value.all? { |e| e.is_a? Array } 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/7KYU/Veg.rb: -------------------------------------------------------------------------------- 1 | def count_vegetables(s) 2 | new_array = [] 3 | new_hash = Hash.new {|value, key| value[key] = 0} 4 | 5 | s.split(" ").each {|x| new_hash[x] += 1} 6 | 7 | new_hash = new_hash.sort_by {|k, v| [v, k] } 8 | #new_hash = new_hash.sort {|a1,a2| a2[1]<=>a1[1]} 9 | #new_hash = new_hash.sort.reverse 10 | new_hash.each do |value,key| 11 | temp = [] 12 | temp << key << value 13 | new_array << temp 14 | end 15 | new_array 16 | end 17 | 18 | p s1 = "potato tofu cucumber cabbage turnip pepper onion carrot celery mushroom potato tofu cucumber cabbage" 19 | count_vegetables(s1) 20 | -------------------------------------------------------------------------------- /Ruby/7KYU/VowelCount.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Return the number (count) of vowels in the given string. 4 | 5 | We will consider a, e, i, o, and u as vowels for this Kata. 6 | 7 | =end 8 | 9 | # My Solution 10 | def getCount(inputStr) 11 | inputStr.chars.count {|c| c =~ /[aeiou]/i } 12 | end 13 | 14 | # Better Solution 15 | def getCount(inputStr) 16 | inputStr.count("aeiou") 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/7KYU/VowelOne.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that takes a string and outputs a strings of 1's 4 | and 0's where vowels become 1's and non-vowels become 0's. 5 | 6 | All non-vowels including non alpha characters (spaces,commas etc.) 7 | should be included. 8 | 9 | =end 10 | 11 | # My Solution 12 | def vowel_one(s) 13 | x = "" 14 | s.each_char {|letter| ['a','e','i','o','u'].include?(letter.downcase) ? x += "1" : x += "0"} 15 | return x 16 | end 17 | 18 | # Better Solution 19 | def vowel_one(s) 20 | s.gsub(/[^aeiou]/i, '0').gsub(/[^0]/, '1') 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/7KYU/XsandOs.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Check to see if a string has the same amount of 'x's and 'o's. The method must 4 | return a boolean and be case insensitive. The string can contains any char. 5 | 6 | Examples input/output: 7 | 8 | XO("ooxx") => true 9 | XO("xooxx") => false 10 | XO("ooxXm") => true 11 | XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true 12 | XO("zzoo") => false 13 | 14 | =end 15 | 16 | # My Solution 17 | def XO(str) 18 | (str.downcase.count "x") == (str.downcase.count "o") ? true : false 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/8KYU/2plus2.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Steve loves MS Windows calculator in Standard mode. 4 | 5 | He tries to calculate 2 + 2 * 2 in his favorite programming language. 6 | But Steve doesn't know about order of operations and he wonders why answers 7 | are different. He expects 8 but gets 6. 8 | 9 | Help Steve to fix program, so result will be identical to MS Windows calculator. 10 | 11 | =end 12 | 13 | # My Solution 14 | def calculate 15 | (2 + 2) * 2 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/AlterCase.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | altERnaTIng cAsE <=> ALTerNAtiNG CaSe 4 | 5 | Define to_alternating_case(char*) such that each lowercase letter becomes 6 | uppercase and each uppercase letter becomes lowercase 7 | 8 | =end 9 | 10 | # My Solution 11 | class String 12 | def to_alternating_case 13 | self.split("").map {|x| x == x.upcase ? x.downcase : x.upcase}.join 14 | end 15 | end 16 | 17 | # Better Solution 18 | class String 19 | def to_alternating_case 20 | swapcase 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/ArrayRemove.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You come across a method for removing all odd numbers from an array 4 | 5 | removing all odd numbers from an array 6 | 7 | def remove_odd_numbers_from_array(a) 8 | a.each do |x| 9 | if x%2!=0 10 | a.delete x 11 | end 12 | end 13 | return a 14 | end 15 | 16 | This method does not work as expected. Can you correct it 17 | 18 | =end 19 | 20 | # My Solution 21 | def remove_odd_numbers_from_array(a) 22 | a.select {|x| x if x.even?} 23 | end 24 | 25 | # Better Solution 26 | def remove_odd_numbers_from_array(a) 27 | a.reject(&:odd?) 28 | end 29 | 30 | # Another Solution 31 | def remove_odd_numbers_from_array(a) 32 | a.select(&:even?) 33 | end 34 | -------------------------------------------------------------------------------- /Ruby/8KYU/ArraySecond.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Define a new instance method on the Array class called second, which returns the 4 | second item in an array (similar to the way .first and .last work in Ruby). 5 | 6 | If there is no element with index 1 in the array, return nil in Ruby, or 7 | undefined in Javascript. 8 | 9 | Ruby Example 10 | 11 | [3, 4, 5].second 12 | # => 4 13 | 14 | [].second 15 | # => nil 16 | Javascript Example 17 | 18 | [1, 2, 3].second(); 19 | // => 2 20 | 21 | [].second(); 22 | // => undefined 23 | 24 | =end 25 | 26 | # My Solution 27 | class Array 28 | def second 29 | self[1] 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /Ruby/8KYU/BLost.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given and array of integers (x), return the array with each 4 | value doubled. Example: 5 | 6 | [1, 2, 3] --> [2, 4, 6] 7 | 8 | For the beginner, try to use the map method 9 | - it comes in very handy quite a lot so is a good one to know. 10 | 11 | =end 12 | 13 | def maps(x) 14 | x.map{|e| e * 2} 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/8KYU/BReduce.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given and array of integers (x), return the result of multiplying 4 | the values together in order. Example: 5 | 6 | [1, 2, 3] --> 6 7 | For the beginner, try to use the reduce method - it comes in 8 | very handy quite a lot so is a good one to know. 9 | 10 | Array will not be empty. 11 | 12 | =end 13 | 14 | def grow(x) 15 | x.map.reduce(:*) 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/Banjo.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function which answers the question "Are you playing Banjo?". If your 4 | name starts with the letter "R" or lower case "r", you are playing Banjo! 5 | 6 | The function takes a name as its only argument, and returns one of the 7 | following strings: 8 | 9 | C# name + "plays banjo" name + "does not play banjo" 10 | 11 | Names given are always valid strings. 12 | 13 | =end 14 | 15 | # My Solution 16 | def are_you_playing_banjo(name) 17 | name[0].downcase == "r" ? "#{name} plays banjo" : "#{name} does not play banjo" 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/8KYU/BasicFunction.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Fix the function 4 | 5 | I created this function to add five to any number that was 6 | passed in to it and return the new value. It doesn't throw 7 | any errors but it returns the wrong number. 8 | 9 | Can you help me fix the function? 10 | 11 | =end 12 | 13 | # My Solution 14 | def addFive(num) 15 | num + 5 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/BasicVariables.rb: -------------------------------------------------------------------------------- 1 | # This code should store "codewa.rs" as a variable 2 | # called "name" but it's not working. Can you figure out why? 3 | 4 | a = "code" 5 | b = "wa.rs" 6 | name = a + b 7 | -------------------------------------------------------------------------------- /Ruby/8KYU/Bonus.rb: -------------------------------------------------------------------------------- 1 | =begin It's bonus time in the big city! The fatcats are 2 | rubbing their paws in anticipation... but who is going 3 | to make the most money? 4 | 5 | Build a function that takes in two arguments (salary, bonus). 6 | Salary will be an integer, and bonus a boolean. 7 | 8 | If bonus is true, the salary should be multiplied by 10. 9 | If bonus is false, the fatcat did not make enough money 10 | and must receive only his stated salary. 11 | 12 | =end 13 | 14 | # My solution 15 | def bonus_time(salary, bonus) 16 | bonus == true ? "$" + (salary*10).to_s : "$" + salary.to_s 17 | end 18 | 19 | # Better solution 20 | def bonus_time(salary, bonus) 21 | "$#{bonus ? salary * 10 : salary}" 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/BoolYN.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the bool_to_word (Javascript: boolToWord ) method. 4 | 5 | Given: a boolean value 6 | 7 | Return: a 'Yes' string for true and a 'No' string for false 8 | 9 | =end 10 | 11 | # My Solution 12 | def bool_to_word bool 13 | bool ? 'Yes' : 'No' 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/8KYU/BrokenGreetings.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The code does not execute properly. Try to figure out why. 4 | 5 | =end 6 | 7 | class Person 8 | def initialize(name) 9 | @name = name 10 | end 11 | 12 | def greet(other_name) 13 | "Hi #{other_name}, my name is #{name}" 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/8KYU/ConvertArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function to split a string and convert it into an array of words. 4 | 5 | e.g. 6 | 7 | string_to_array("Robin Singh") => ["Robin", "Singh"] 8 | 9 | string_to_array("I love arrays they are my favorite") => ["I", "love", "arrays", "they", "are", "my", "favorite"] 10 | 11 | =end 12 | 13 | def string_to_array(string) 14 | string.split(" ") 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/8KYU/ConvertDigits.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a random number: 4 | 5 | C#: long; 6 | C++: unsigned long; 7 | You have to return the digits of this number within an array in reverse order. 8 | Example: 9 | 10 | 348597 => [7,9,5,8,4,3] 11 | 12 | =end 13 | 14 | # My Solution 15 | def digitize(n) 16 | result = n.to_s.split("").reverse 17 | result.map {|x| x.to_i} 18 | end 19 | 20 | # Better Solution 21 | def digitize(n) 22 | n.to_s.chars.reverse.map(&:to_i) 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/CountByX.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function with two arguments that will return a list of length (n) with 4 | multiples of (x). 5 | 6 | Assume both the given number and the number of times to count will be positive 7 | numbers greater than 0. 8 | 9 | Return the results as an array (or list in Python, Haskell or Elixir). 10 | 11 | Examples: 12 | 13 | count_by(1,10) #should return [1,2,3,4,5,6,7,8,9,10] 14 | count_by(2,5) #should return [2,4,6,8,10] 15 | 16 | =end 17 | 18 | # My Solution 19 | def count_by(x, n) 20 | (1..n).each.map {|y| y*x} 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/8KYU/CountMonkeys.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You take your son to the forest to see the monkeys. 4 | You know that there are a certain number there (n), 5 | but your son is too young to just appreciate the full number, 6 | he has to start counting them from 1. 7 | 8 | As a good parent, you will sit and count with him. 9 | Given the number (n), populate an array with all 10 | numbers up to and including that number, but excluding zero. 11 | 12 | For example, if n = 10: 13 | 14 | return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 15 | 16 | =end 17 | 18 | # My solution 19 | def monkey_count(n) 20 | (1..n).to_a 21 | end 22 | 23 | # Better solution 24 | def monkey_count(n) 25 | [*1..n] 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/8KYU/DNA.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Deoxyribonucleic acid, DNA is the primary information storage molecule in 4 | biological systems. It is composed of four nucleic acid bases Guanine ('G'), 5 | Cytosine ('C'), Adenine ('A'), and Thymine ('T'). 6 | 7 | Ribonucleic acid, RNA, is the primary messenger molecule in cells. RNA differs 8 | slightly from DNA its chemical structure and contains no Thymine. In RNA Thymine 9 | is replaced by another nucleic acid Uracil ('U'). 10 | 11 | Create a funciton which translates a given DNA string into RNA. 12 | 13 | For example: 14 | 15 | DNAtoRNA("GCAT") returns ("GCAU") 16 | 17 | =end 18 | 19 | # My Solution 20 | def DNAtoRNA(dna) 21 | dna.gsub("T","U") 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/DebugHello.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The starship Enterprise has run into some problem when creating a program to 4 | greet everyone as they come aboard. It is your job to fix the code and get the 5 | program working again! 6 | 7 | Example output: 8 | 9 | Hello, Mr. Spock 10 | 11 | =end 12 | 13 | # My Solution 14 | def say_hello(name) 15 | "Hello, #{name}" 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/DecimalPlace.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Each number should be formatted that it is rounded to two decimal places. 4 | You don't need to check whether the input is a valid number because only valid 5 | numbers are used in the tests. 6 | 7 | Example: 8 | 5.5589 is rounded 5.56 9 | 3.3424 is rounded 3.34 10 | 11 | =end 12 | 13 | # My Solution 14 | def two_decimal_places(n) 15 | n.round(2) 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/Division.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This method, which is supposed to return the result of dividing its first 4 | argument by its second, isn't always returning correct values. Fix it. 5 | 6 | =end 7 | 8 | # My Solution 9 | def divide_numbers x, y 10 | x.to_f / y 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/8KYU/DoubleChar.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a string, you have to return a string in which each character 4 | (case-sensitive) is repeated once. 5 | 6 | double_char("String") ==> "SSttrriinngg" 7 | 8 | double_char("Hello World") ==> "HHeelllloo WWoorrlldd" 9 | 10 | double_char("1234!_ ") ==> "11223344!!__ " 11 | 12 | =end 13 | 14 | # My Solution 15 | def double_char(str) 16 | str.split("").map {|x| x * 2}.join 17 | end 18 | 19 | # Another Solution 20 | def double_char(str) 21 | str.chars.map{|x| x*2}.join 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/DuckDuckGoose.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The objective of 'Duck, duck, goose' is to walk in a circle, tapping on each 4 | player's head until one is finally chosen. 5 | 6 | Task: Given an array of Player objects (an array of associative arrays in PHP) 7 | and an index (1-based), return the name of the chosen Player. 8 | 9 | Example: 10 | 11 | duck_duck_goose([a, b, c, d], 1) should return a.name 12 | duck_duck_goose([a, b, c, d], 5) should return a.name 13 | duck_duck_goose([a, b, c, d], 4) should return d.name 14 | 15 | =end 16 | 17 | # My Solution 18 | def duck_duck_goose(players, goose) 19 | players[(goose % players.size) - 1].name 20 | end 21 | 22 | # Another Solution 23 | def duck_duck_goose(players, goose) 24 | players.cycle.take(goose).last.name 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/8KYU/DutyFree.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The purpoe of this kata is to work out just how many bottles of duty free 4 | whiskey you would have to buy such that the saving over the normal high street 5 | price would effectively cover the cost of your holiday. 6 | 7 | You will be given the high street price (normPrice), the duty free discount 8 | (discount) and the cost of the holiday. 9 | 10 | For example, if a bottle cost £10 normally and the discount in duty free was 11 | 10%, you would save £1 per bottle. If your holiday cost £500, the answer you 12 | should return would be 500. 13 | 14 | All inputs will be integers. Please return an integer. Round down. 15 | 16 | =end 17 | 18 | # My Solution 19 | def duty_free(price, discount, holiday_cost) 20 | (holiday_cost.to_f / (discount / 100.0 * price)).floor 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/8KYU/EvenOdd.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function that takes an integer as an argument and 4 | returns "Even" for even numbers or "Odd" for odd numbers. 5 | 6 | =end 7 | 8 | # My solution 9 | def even_or_odd(number) 10 | number.even? ? "Even" : "Odd" 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/8KYU/FakeBinary.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given a string of numbers, you should replace any number below 5 with '0' 4 | and any number 5 and above with '1'. Return the resulting string. 5 | 6 | =end 7 | 8 | # My Solution 9 | def fake_bin(s) 10 | answer = "" 11 | s.split("").each {|a| answer += a.to_i < 5 ? "0" : "1"} 12 | answer 13 | end 14 | 15 | # Better Solution 16 | def fake_bin(s) 17 | s.tr('0-45-9','000001') 18 | end 19 | 20 | # Another Solution 21 | def fake_bin(s) 22 | s.gsub(/[0-4]/,'0').gsub(/[5-9]/, '1') 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/Frog.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The wide mouth frog is particularly interested in the eating 4 | habits of other creatures. 5 | 6 | He just can't stop asking the creatures he encounters what 7 | they like to eat. But then he meet the alligator who just 8 | LOVES to eat wide-mouthed frogs! 9 | 10 | When he meets the alligator, it then makes a tiny mouth. 11 | 12 | Your goal in this kata is to create complete the mouth_size 13 | method this method take one argument animal which corresponds 14 | to the animal encountered by frog. If this one is an alligator 15 | (case insensitive) return small otherwise return wide. 16 | 17 | =end 18 | 19 | def mouth_size(animal) 20 | return animal.downcase != 'alligator' ? 'wide' : 'small' 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/8KYU/GetNum.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function which removes from string all non-digit characters and parse 4 | the remaining to number. E.g: "hell5o wor6ld" -> 56 5 | 6 | Function: 7 | 8 | get_number_from_string(s) 9 | 10 | =end 11 | 12 | # My Solution 13 | def get_number_from_string(s) 14 | s.gsub(/[^\d]/,"").to_i 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/8KYU/GrassCombine.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function named combineNames(combine_names in python, ruby) 4 | that accepts two parameters (first and last name). The function 5 | should return the full name. 6 | 7 | Example: 8 | 9 | combine_names('James', 'Stevens') 10 | returns: 11 | 12 | 'James Stevens' 13 | 14 | =end 15 | 16 | # My solution 17 | def combine_names(name1, name2) 18 | name1 + ' ' + name2 19 | end 20 | 21 | # Better solution 22 | def combine_names(*names) 23 | names.join(" ") 24 | end 25 | -------------------------------------------------------------------------------- /Ruby/8KYU/Grasshopper.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Personalized greeting 4 | 5 | Create a function that gives a personalized greeting. 6 | This function takes two parameters: name and owner. 7 | 8 | Use conditionals to return the proper message: 9 | 10 | case return 11 | name equals owner 'Hello boss' 12 | otherwise 'Hello guest' 13 | 14 | =end 15 | 16 | # Condition to check if name = owner 17 | # If TRUE return 'Hello boss' 18 | # Else return 'Hello guest' 19 | 20 | def greet(name,owner) 21 | name == owner ? 'Hello boss' : 'Hello guest' 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/HelloWorld.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Make a simple function called greet that returns the most-famous "hello world!". 4 | 5 | Style Points 6 | 7 | Sure, this is about as easy as it gets. But how clever can you be to create the 8 | most creative hello world you can think of? What is a "hello world" solution you 9 | would want to show your friends? 10 | 11 | =end 12 | 13 | # My Solution 14 | def greet 15 | "hello world!" 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/Hydrated.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Nathan loves cycling. 4 | 5 | Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres 6 | of water per hour of cycling. 7 | 8 | You get given the time and you need to return the number of litres Nathan 9 | will drink, rounded to the smallest value. 10 | 11 | For example: 12 | 13 | time = 3 ----> litres = 1 14 | 15 | time = 6.7---> litres = 3 16 | 17 | time = 11.8--> litres = 5 18 | 19 | =end 20 | 21 | # My Solution 22 | def litres(time) 23 | (time * 0.5).floor 24 | end 25 | 26 | # Better Solution 27 | def litres(time) 28 | time.div(2) 29 | end 30 | 31 | # Another Solution 32 | def litres(time) 33 | (time / 2).floor 34 | end 35 | -------------------------------------------------------------------------------- /Ruby/8KYU/IfLambda.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Who likes keywords? Nobody likes keywords, so why use them? 4 | 5 | You know what keyword I use too much? if! We should make a function called _if, 6 | with its arguments as a logical test and two functions/lambdas where the first 7 | function is executed if the boolean is true, and the second if it's false, like 8 | an if/else statement, so that we don't have to mess around with those nasty 9 | keywords! Even so, It should support truthy/falsy types just like the keyword. 10 | 11 | Examples: 12 | 13 | _if(true, proc{puts "True"}, proc{puts "False"}) 14 | # Logs 'True' to the console. 15 | 16 | =end 17 | def _if(bool, ifTrue, ifFalse) 18 | bool ? ifTrue.call : ifFalse.call 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/8KYU/Jennys.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Jenny has written a function that returns a greeting for a user. 4 | However, she's in love with Johnny, and would like to greet him 5 | slightly different. She added a special case to her function, 6 | but she made a mistake. 7 | 8 | Can you help her? 9 | 10 | =end 11 | 12 | def greet(name) 13 | return ( name == "Johnny" ) ? "Hello, my love!" : "Hello, #{name}!" 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/8KYU/MagicNum1.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a method all? (JS: all) which takes an array, and a block (JS: function), 4 | and returns true if the block (/function) returns true for every element in the 5 | array. Otherwise, it should return false. If the array is empty, it should 6 | return true, since technically nothing failed the block (/function) test. 7 | 8 | =end 9 | 10 | # My Solution 11 | def all? array, &block 12 | result = array.select{ |x| x if block.call(x)} 13 | result == array 14 | end 15 | 16 | # Better Solution 17 | def all? array, &block 18 | array.all?(&block) 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/8KYU/MakeChange.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You go to the store and have a 10 dollar bill to spend. 4 | You buy candy, chips, and soda. Find out how much change 5 | you get back from the cashier. 6 | 7 | Item Cost 8 | Candy $1.42 9 | Chips $2.40 10 | Soda $1.00 11 | Create 5 variables and use the cost from the table above to set their values. 12 | 13 | money 14 | candy 15 | chips 16 | soda 17 | change 18 | 19 | =end 20 | 21 | # My solution 22 | money = 10 23 | candy = 1.42 24 | chips = 2.4 25 | soda = 1 26 | change = money - candy - chips - soda 27 | -------------------------------------------------------------------------------- /Ruby/8KYU/MaxandMin.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your task is to make two functions, max and min (maximum and minimum in PHP) 4 | that take a(n) array/vector of integers list as input and outputs, respectively, 5 | the largest and lowest number in that array/vector. 6 | 7 | #Examples 8 | 9 | max([4,6,2,1,9,63,-134,566]) returns 566 10 | min([-52, 56, 30, 29, -54, 0, -110]) returns -110 11 | max([5]) returns 5 12 | min([42, 54, 65, 87, 0]) returns 0 13 | #Notes 14 | 15 | You may consider that there will not be any empty arrays/vectors. 16 | 17 | =end 18 | 19 | # My Solution 20 | def min(list) 21 | list.sort.first 22 | end 23 | 24 | def max(list) 25 | list.sort.last 26 | end 27 | 28 | # Better Solution 29 | def min(list) 30 | list.min 31 | end 32 | 33 | def max(list) 34 | list.max 35 | end 36 | -------------------------------------------------------------------------------- /Ruby/8KYU/MeanOfArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | It's the academic year's end, fateful moment of your school report. The averages 4 | must be calculated. All the students come to you and entreat you to calculate 5 | their average for them. Easy ! You just need to write a script. 6 | 7 | Return the average of the given array rounded down to its nearest integer. 8 | 9 | The array will never be empty. 10 | 11 | =end 12 | 13 | # My Solution 14 | def get_average(marks) 15 | marks.inject(&:+)/marks.size 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/Multiply.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The code does not execute properly. Try to figure out why. 4 | 5 | =end 6 | 7 | def multiply(a b) 8 | a * b 9 | end 10 | -------------------------------------------------------------------------------- /Ruby/8KYU/Multiply2Num.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This function has to be called multiply and needs to take two numbers as 4 | arguments, and has to return the multiplication of the two arguments. 5 | 6 | =end 7 | 8 | # My Solution 9 | def multiply(*args) 10 | args[0] * args[1] 11 | end 12 | 13 | # Better Solution 14 | def multiply (*nums) 15 | nums.reduce(:*) 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/MyTail.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Some new animals have arrived at the zoo. The zoo keeper is concerned that 4 | perhaps the animals do not have the right tails. To help her, you must correct 5 | the broken function to make sure that the second argument (tail), is the same 6 | as the last letter of the first argument (body) 7 | otherwise the tail wouldn't fit! 8 | 9 | If the tail is right return true, else return false. 10 | 11 | The arguments will always be strings, and normal letters. 12 | 13 | =end 14 | 15 | # My Solution 16 | def correct_tail(body, tail) 17 | body[-1] == tail ? true : false 18 | end 19 | 20 | # Better Solution 21 | def correct_tail(body, tail) 22 | body[-1] == tail 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/Needle.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Can you find the needle in the haystack? 4 | 5 | Write a function findNeedle() that takes an array full of junk 6 | but containing one "needle" 7 | 8 | After your function finds the needle it should return a message 9 | (as a string) that says: 10 | 11 | "found the needle at position " plus the index it found the needle 12 | 13 | So 14 | 15 | find_needle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk']) 16 | 17 | should return 18 | 19 | 'found the needle at position 5' 20 | 21 | =end 22 | 23 | # return position of array index 24 | 25 | def find_needle(haystack) 26 | "found the needle at position #{haystack.index('needle')}" 27 | end 28 | -------------------------------------------------------------------------------- /Ruby/8KYU/NumtoString.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | We need a function that can transform a number into a string. 4 | 5 | What ways of achieving this do you know? 6 | 7 | =end 8 | 9 | # My Solution 10 | def numberToString(num) 11 | num.to_s 12 | end 13 | -------------------------------------------------------------------------------- /Ruby/8KYU/OnesandZeros.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given an array of one's and zero's convert the equivalent binary value to an integer. 4 | 5 | Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1 6 | 7 | Examples: 8 | 9 | Testing: [0, 0, 0, 1] ==> 1 10 | Testing: [0, 0, 1, 0] ==> 2 11 | Testing: [0, 1, 0, 1] ==> 5 12 | Testing: [1, 0, 0, 1] ==> 9 13 | Testing: [0, 0, 1, 0] ==> 2 14 | Testing: [0, 1, 1, 0] ==> 6 15 | Testing: [1, 1, 1, 1] ==> 15 16 | Testing: [1, 0, 1, 1] ==> 11 17 | 18 | =end 19 | 20 | # Join the array together using .join 21 | # Convert to an Integer using .to_i 22 | 23 | def binary_array_to_number(arr) 24 | arr.join("").to_i(2) 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/8KYU/OppNum.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Very simple, given a number, find its opposite. 4 | 5 | Examples: 6 | 7 | 1: -1 8 | 14: -14 9 | -34: 34 10 | But can you do it in 1 line of code and without any conditionals? 11 | 12 | =end 13 | 14 | # My Solution 15 | def opposite(num) 16 | -num 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/8KYU/OpposeiteAttract.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Timmy & Sarah think they are in love, but around where they live, they will only 4 | know once they pick a flower each. If one of the flowers has an even number of 5 | petals and the other has an odd number of petals it means they are in love. 6 | 7 | Write a function that will take the number of petals of each flower and return 8 | true if they are in love and false if they aren't. 9 | 10 | =end 11 | 12 | # My Solution 13 | def lovefunc( f1, f2 ) 14 | (f1.even? && f2.odd?) || (f1.odd? && f2.even?) ? true : false 15 | end 16 | 17 | # Better Solution 18 | def lovefunc( flower1, flower2 ) 19 | (flower1 + flower2).odd? 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/8KYU/PalStrings.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | A palindrome is a word, phrase, number, or other sequence of characters which 4 | reads the same backward or forward. Allowances may be made for adjustments to 5 | capital letters, punctuation, and word dividers. 6 | 7 | true == is_palindrome("anna") 8 | false == is_palindrome("walter") 9 | 10 | =end 11 | 12 | # My Solution 13 | def is_palindrome(str) 14 | str.to_s == str.to_s.reverse 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/8KYU/Planet.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | The function is not returning the correct values. Can you figure out why? 4 | 5 | get_planet_name(3) # should return 'Earth' 6 | get_planet_name(3) # should return 'Earth' 7 | 8 | =end 9 | 10 | # My Solution 11 | def get_planet_name(id) 12 | case id 13 | when 1; name = "Mercury" 14 | when 2; name = "Venus" 15 | when 3; name = "Earth" 16 | when 4; name = "Mars" 17 | when 5; name = "Jupiter" 18 | when 6; name = "Saturn" 19 | when 7; name = "Uranus" 20 | when 8; name = "Neptune" 21 | else ; name = "" 22 | end 23 | name 24 | end 25 | 26 | # Better Solution 27 | def get_planet_name(id) 28 | %w[0 Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune][id] 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/8KYU/Plural.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | We need a simple function that determines if a plural is needed or not. It 4 | should take a number, and return true if a plural should be used with that 5 | number or false if not. This would be useful when printing out a string such as 6 | 5 minutes, 14 apples, or 1 sun. 7 | 8 | You only need to worry about english grammar rules for this kata, where anything 9 | that isn't singular (one of something), it is plural (not one of something). 10 | All values will be positive integers or floats, or zero. 11 | 12 | =end 13 | 14 | # My Solution 15 | def plural(n) 16 | n != 1 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/8KYU/Powerof2.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function powersOfTwo which will return list of all powers of 2 from 0 to 4 | n (where n is an exponent). 5 | 6 | E.g: 7 | 8 | n = 0 -> 2^0 -> [1] 9 | 10 | n = 1 -> 2^0, 2^1 -> [1,2] 11 | 12 | n = 2 -> 2^0, 2^1, 2^2 -> [1,2,4] 13 | 14 | =end 15 | 16 | # My Solution 17 | def powers_of_two(n) 18 | 0.upto(n).map {|x| 2**x} 19 | end 20 | 21 | # Another Solution 22 | def powers_of_two(n) 23 | (0..n).map {|k| 2**k} 24 | end 25 | -------------------------------------------------------------------------------- /Ruby/8KYU/PreFizzBuzz.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This is the first step to understanding FizzBuzz. 4 | 5 | Your inputs: a positive integer, n, greater than or equal to one. n is provided, 6 | you have NO CONTROL over its value. 7 | 8 | Your expected outputs: an array of positive integers from 1 to n 9 | 10 | Your job is to write an algorithm that gets you from the input to the output. 11 | 12 | =end 13 | 14 | # My Solution 15 | def pre_fizz(n) 16 | 1.upto(n).map {|x| x} 17 | end 18 | 19 | # Better Solution 20 | def pre_fizz(n) 21 | (1..n).to_a 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/RegBallSuperBall.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a class Ball. 4 | 5 | Ball objects should accept one argument for "ball type" when instantiated. 6 | 7 | If no arguments are given, ball objects should instantiate with a "ball type" 8 | of "regular." 9 | 10 | ball1 = Ball.new 11 | ball2 = Ball.new "super" 12 | ball1.ball_type #=> "regular" 13 | ball2.ball_type #=> "super" 14 | =end 15 | 16 | # My Solution 17 | class Ball 18 | attr_accessor :ball_type 19 | 20 | def initialize(type="regular") 21 | @ball_type = type 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/RemoveElements.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Take an array and remove every second element out of that array. Always keep the 4 | first element and start removing with the next element. 5 | 6 | Example: 7 | 8 | my_arr = ['Keep', 'Remove', 'Keep', 'Remove', 'Keep', ...] 9 | None of the arrays will be empty, so you don't have to worry about that! 10 | 11 | =end 12 | 13 | # My Solution 14 | def remove_every_other(arr) 15 | result = [] 16 | arr.each_with_index {|x,i| result << x if i.even?} 17 | result 18 | end 19 | 20 | # Better Solution 21 | def remove_every_other(arr) 22 | arr.select.with_index { |_,idx| idx.even? } 23 | end 24 | 25 | # Another Solution 26 | def remove_every_other(arr) 27 | arr.each_slice(2).map(&:first) 28 | end 29 | -------------------------------------------------------------------------------- /Ruby/8KYU/RemoveMark.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | Remove a exclamation mark from the end of string. For a beginner kata, you can 3 | assume that the input data is always a string, no need to verify it. 4 | Examples 5 | 6 | remove("Hi!") === "Hi" 7 | remove("Hi!!!") === "Hi!!" 8 | remove("!Hi") === "!Hi" 9 | remove("!Hi!") === "!Hi" 10 | remove("Hi! Hi!") === "Hi! Hi" 11 | remove("Hi") === "Hi" 12 | Note 13 | 14 | Please don't post issue about difficulty or duplicate. 15 | 16 | =end 17 | 18 | # My Solution 19 | def remove(s) 20 | s[-1] == '!' ? s[0...-1] : s 21 | end 22 | 23 | # Better Solution 24 | def remove(s) 25 | s.chomp('!') 26 | end 27 | -------------------------------------------------------------------------------- /Ruby/8KYU/RemoveMarks.rb: -------------------------------------------------------------------------------- 1 | # Write function removeExclamationMarks which removes all exclamation 2 | # marks from a given string. 3 | 4 | def remove_exclamation_marks(s) 5 | s.delete('!') 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/8KYU/RemoveSpaces.rb: -------------------------------------------------------------------------------- 1 | # Simple, remove the spaces from the string, then return the 2 | # resultant string. 3 | 4 | def no_space(x) 5 | x.gsub(" ","") 6 | end 7 | -------------------------------------------------------------------------------- /Ruby/8KYU/Removechars.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | It's pretty straightforward. Your goal is to create a function that removes 4 | the first and last characters of a string. You're given one parameter. 5 | 6 | =end 7 | 8 | # My Solution 9 | def remove_char(s) 10 | s[1..(s.size - 2)] 11 | end 12 | 13 | # Better Solution 14 | def remove_char(s) 15 | s[1..-2] 16 | end 17 | 18 | # Another Solution 19 | def remove_char(s) 20 | s[1...-1] 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/8KYU/ReplaceVowel.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Replace all vowel to exclamation mark in the sentence. aeiouAEIOU is vowel. 4 | 5 | Examples 6 | replace("Hi!") === "H!!" 7 | replace("!Hi! Hi!") === "!H!! H!!" 8 | replace("aeiou") === "!!!!!" 9 | replace("ABCDE") === "!BCD!" 10 | 11 | =end 12 | 13 | # My Solution 14 | def replace(s) 15 | s.gsub(/[aeiouAEIOU]/, "!") 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/ReturnHigh.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | In this kata, your job is to return the two highest values in a list, this 4 | doesn't include duplicates. 5 | 6 | When given an empty list, you should also return an empty list, no strings will 7 | be passed into the list. 8 | 9 | The return should also be ordered from highest to lowest. 10 | 11 | If the argument passed isn't a list, you should return false. 12 | 13 | Examples: 14 | 15 | two_highest([4, 10, 10, 9]) should return [10, 9] 16 | 17 | two_highest([]) should return [] 18 | 19 | two_highest("test") should return False 20 | 21 | =end 22 | 23 | # My Solution 24 | def two_highest(list) 25 | (list.is_a? Array) ? list.sort.reverse.uniq[0..1] : false 26 | end 27 | 28 | # Better Solution 29 | def two_highest(list) 30 | list.class != Array ? false : list.max(2) 31 | end 32 | -------------------------------------------------------------------------------- /Ruby/8KYU/Reverse.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You're at the zoo... all the meerkats look weird. Something has gone terribly 4 | wrong - someone has gone and switched their heads and tails around! 5 | 6 | Save the animals by switching them back. You will be given an array which will 7 | have three values (tail, body, head). It is your job to re-arrange the array so 8 | that the animal is the right way round (head, body, tail). 9 | 10 | Same goes for all the other arrays/lists that you will get in the tests: you 11 | have to change the element positions with the same exact logics - simples! 12 | 13 | =end 14 | 15 | # My Solution 16 | def fix_the_meerkat(arr) 17 | arr.reverse 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/8KYU/ReverseString.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You need to write a function that reverses the words in a given string. If this 4 | is not clear enough, here are some examples: 5 | 6 | =end 7 | 8 | # My Solution 9 | def reverse(string) 10 | string.split.reverse.join(' ') 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/8KYU/Saber.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Inspired by the development team at Vooza, write the 4 | function howManyLightsabersDoYouOwn that 5 | 6 | accepts the name of a programmer, and 7 | returns the number of lightsabers owned by that person. 8 | The only person who owns lightsabers is Zach, by the way. 9 | He owns 18, which is an awesome number of lightsabers. Anyone else owns 0. 10 | 11 | No starting help here -- you'll need to know how to write a function 12 | that accepts a parameter and returns a value based on that parameter. 13 | 14 | HowManyLightsabersDoYouOwn("Adam") // => 0 15 | HowManyLightsabersDoYouOwn("Zach") // => 18 16 | 17 | =end 18 | 19 | # My Solution 20 | def howManyLightsabersDoYouOwn(name="") 21 | return name.downcase == "zach" ? 18 : 0 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/Sanity.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | This function returns something strange. What's wrong? 4 | 5 | =end 6 | 7 | # My Solution 8 | def mystery() 9 | results = {'sanity' => 'Hello'} 10 | return results 11 | end 12 | -------------------------------------------------------------------------------- /Ruby/8KYU/Sheep.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Consider an array of sheep where some sheep may be missing from their place. 4 | We need a function that counts the number of sheep present in the array 5 | (true means present). 6 | 7 | For example, 8 | 9 | The correct answer would be 17. 10 | 11 | Hint: Don't forget to check for bad values like null/undefined 12 | 13 | =end 14 | 15 | # My Solution 16 | def countSheeps array 17 | array.count {|x| x==true} 18 | end 19 | 20 | # Better Solution 21 | def countSheeps array 22 | array.count(true) 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/ShortLongShort.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given 2 strings, a and b, return a string of the form 4 | short+long+short, with the shorter string on the outside 5 | and the longer string on the inside. The strings will 6 | not be the same length, but they may be empty (length0). 7 | 8 | For example: 9 | 10 | solution("1", "22") # returns "1221" 11 | solution("22", "1") # returns "1221" 12 | 13 | =end 14 | 15 | def solution(a, b) 16 | a.length < b.length ? (a+b+a).to_s : (b+a+b).to_s 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/8KYU/SixToast.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You forgot to count the number of toast you put into there, you don't know 4 | if you put exactly six pieces of toast into the toasters. 5 | 6 | =end 7 | 8 | # My Solution 9 | def six_toast(num) 10 | num > 5 ? num - 6 : num 11 | end 12 | 13 | # Better Solution 14 | def six_toast(num) 15 | (6-num).abs 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/Smash.rb: -------------------------------------------------------------------------------- 1 | def smash(words) 2 | words.join(" ") 3 | end -------------------------------------------------------------------------------- /Ruby/8KYU/SortandStar.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You will be given an array (s). You must sort it alphabetically 4 | (case-sensitive!!) and then return the first value. 5 | 6 | The returned value must be a string, and have '***' between 7 | each of its letters. 8 | 9 | =end 10 | 11 | # My solution 12 | def two_sort(s) 13 | word = "" 14 | s.sort[0].each_char {|a| word += a + "***"} 15 | return word = word [0..word.length-4] 16 | end 17 | 18 | # Better solution 19 | def two_sort(s) 20 | s.min.chars.join('***') 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/8KYU/SplitArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a method partition that accepts a list and a block. It should return two 4 | arrays: the first, with all the elements for which the given block returned 5 | true, and the second for the remaining elements. 6 | 7 | Here's a simple example: 8 | 9 | animals = ["cat", "dog", "duck", "cow", "donkey"] 10 | partition(animals){|animal| animal.size == 3} 11 | #=> [["cat", "dog", "cow"], ["duck", "donkey"]] 12 | 13 | =end 14 | 15 | # My Solution 16 | def partition list, &block 17 | a1 = [] ; a2 = [] 18 | list.each {|x| yield(x) ? a1 << x : a2 << x } 19 | [a1, a2] 20 | end 21 | 22 | # Better Solution 23 | def partition list, &block 24 | list.partition(&block) 25 | end 26 | -------------------------------------------------------------------------------- /Ruby/8KYU/SquareSum.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the squareSum method so that it squares each number passed into it and 4 | then sums the results together. 5 | 6 | For example: 7 | 8 | squareSum([1, 2, 2]) # should return 9 9 | 10 | =end 11 | 12 | # My Solution 13 | def squareSum(numbers) 14 | num = 0 15 | numbers.map {|x| num+=x*x} 16 | num 17 | end 18 | 19 | # Better Solution 20 | def squareSum(numbers) 21 | numbers.map {|n| n*n}.reduce(:+) 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/8KYU/SquashBugs.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Simple challenge - eliminate all bugs from the supplied 4 | code so that the code runs and outputs the expected value. 5 | Output should be the length of the longest word, as a number. 6 | 7 | There will only be one 'longest' word. 8 | 9 | =end 10 | 11 | # My solution 12 | def find_longest(string) 13 | spl = string.split(" ") 14 | longest = 0 15 | i=0 16 | while (i < spl.length) do 17 | if (spl[i].length > longest) 18 | longest = spl[i].length 19 | end 20 | i += 1 21 | end 22 | return longest 23 | end 24 | 25 | # Better solution 26 | def find_longest(string) 27 | string.split.map(&:length).max 28 | end 29 | -------------------------------------------------------------------------------- /Ruby/8KYU/StringBugFix.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Oh no! Timmy hasn't followed instructions very carefully and forgot how to use 4 | the new String Template feature, Help Timmy with his string template so it works 5 | as he expects! 6 | 7 | =end 8 | 9 | # My Solution 10 | def build_string(*args) 11 | "I like #{args.join(", ")}!" 12 | end 13 | -------------------------------------------------------------------------------- /Ruby/8KYU/StringRepeat.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function called repeatStr which repeats 4 | the given string string exactly n times. 5 | 6 | =end 7 | 8 | def repeat_str (n, s) 9 | s*n 10 | end 11 | -------------------------------------------------------------------------------- /Ruby/8KYU/StringUpcase.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a method is_uppercase() to see whether the string is ALL CAPS. 4 | For example: 5 | 6 | Corner Cases 7 | 8 | For simplicity, you will not be tested on the ability to handle corner cases 9 | (e.g. "%*&#()%&^#" or similar strings containing alphabetical characters at all) 10 | - an ALL CAPS (uppercase) string will simply be defined as one containing no 11 | lowercase letters. Therefore, according to this definition, strings with no 12 | alphabetical characters (like the one above) should return True. 13 | 14 | =end 15 | 16 | # My Solution 17 | class String 18 | def is_upcase? 19 | self.split("").each {|x| return false if x != x.upcase} 20 | true 21 | end 22 | end 23 | 24 | # Better Solution 25 | class String 26 | def is_upcase? 27 | self == upcase 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/8KYU/StringyStrings.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | write me a function stringy that takes a size and returns a string of alternating '1s' and '0s'. 4 | 5 | the string should start with a 1. 6 | 7 | a string with size 6 should return :'101010'. 8 | 9 | with size 4 should return : '1010'. 10 | 11 | with size 12 should return : '101010101010'. 12 | 13 | The size will always be positive and will only use whole numbers. 14 | 15 | =end 16 | 17 | # My Solution 18 | def stringy(size) 19 | (1..size).map {|x| x % 2}.join 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/8KYU/SumHighLow.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Sum all the numbers of the array (in F# you get a list) except the highest 4 | and the lowest element (the value, not the index!). 5 | (The highest/lowest element is respectively only one element at each edge, 6 | even if there are more than one with the same value!) 7 | 8 | Example: 9 | 10 | { 6, 2, 1, 8, 10 } => 16 11 | { 1, 1, 11, 2, 3 } => 6 12 | 13 | 14 | If array is empty, null or None, or if only 1 Element exists, return 0. 15 | 16 | =end 17 | 18 | # My Solution 19 | def sum_array(arr) 20 | return 0 if arr == nil 21 | sum = 0 22 | arr.sort! 23 | 1.upto(arr.length-2){ |x| sum += arr[x]} 24 | sum 25 | end 26 | 27 | # Better Solution 28 | def sum_array(arr) 29 | arr ? (arr.size > 2 ? arr.sort[1...-1].inject(:+) : 0) : 0 30 | end 31 | -------------------------------------------------------------------------------- /Ruby/8KYU/SumMixArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given an array of integers as strings and numbers, return the sum of the array 4 | values as if all were numbers. 5 | 6 | Return your answer as a number. 7 | 8 | =end 9 | 10 | # My Solution 11 | def sum_mix(x) 12 | n = 0 13 | x.each {|x| n += x.to_i} 14 | n 15 | end 16 | 17 | # Better Solution 18 | def sum_mix(x) 19 | x.map(&:to_i).inject(:+) 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/8KYU/Summation.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a program that finds the summation of every number between 1 and num. 4 | The number will always be a positive integer greater than 0. 5 | 6 | For example: 7 | 8 | summation(2) -> 3 9 | 1 + 2 10 | 11 | summation(8) -> 36 12 | 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 13 | 14 | =end 15 | 16 | # My Solution 17 | def summation(num) 18 | (1..num).inject(&:+) 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/8KYU/SumofPositive.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | You get an array of numbers, return the sum of all of 4 | the positives ones. 5 | 6 | Example [1,-4,7,12] => 1 + 7 + 12 = 20 7 | 8 | Note: array may be empty, in this case return 0. 9 | 10 | =end 11 | 12 | # Inject the array using .Inject 13 | # If Statement if its > 0 then sum += n else sum = sum 14 | 15 | def positive_sum(arr) 16 | arr.inject(0) {|sum, n| n > 0 ? sum += n : sum = sum} 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/8KYU/SwitchItUp.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | When provided with a number between 0-9, return it in words. 4 | 5 | Input :: 1 6 | 7 | Output :: "One". 8 | 9 | Try using "Switch" statements. 10 | 11 | =end 12 | 13 | # My Solution 14 | def switch_it_up(number) 15 | %w"Zero One Two Three Four Five Six Seven Eight Nine"[number] 16 | end 17 | -------------------------------------------------------------------------------- /Ruby/8KYU/UEFA.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Finish the uefaEuro2016() function so it return string just 4 | like in the examples below: 5 | 6 | uefaEuro2016(['Germany', 'Ukraine'],[2, 0]) // "At match Germany - Ukraine, Germany won!" 7 | uefaEuro2016(['Belgium', 'Italy'],[0, 2]) // "At match Belgium - Italy, Italy won!" 8 | uefaEuro2016(['Portugal', 'Iceland'],[1, 1]) // "At match Portugal - Iceland, teams played draw." 9 | 10 | =end 11 | 12 | def uefa_euro_2016(teams, scores) 13 | "At match #{teams[0]} - #{teams[1]}, #{scores[0] > scores[1] ? teams[0] + " won!" : scores[0] == scores[1] ? "teams played draw." : teams[1] + " won!"}" 14 | end 15 | -------------------------------------------------------------------------------- /Ruby/8KYU/Vacation.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | After a hard quarter in the office you decide to get some rest on a vacation. 4 | So you will book a flight for you and your girlfriend and try to leave all the 5 | mess behind you. 6 | 7 | You will need a rental car in order for you to get around in your vacation. 8 | The manager of the car rental makes you some good offers. 9 | 10 | Every day you rent the car costs $40. If you rent the car for 7 or more days, 11 | you get $50 off your total. Alternatively, if you rent the car for 3 or more 12 | days, you get $20 off your total. 13 | 14 | Write a code that gives out the total amount for different days(d). 15 | 16 | =end 17 | 18 | # My Solution 19 | def rental_car_cost(d) 20 | total = d * 40 21 | return total -= 50 if d > 6 22 | d > 2 ? (return total -= 20) : total 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/VolumeCuboid.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Bob needs a fast way to calculate the volume of a cuboid with three values: 4 | length, width and the height of the cuboid. Write a function to help Bob with 5 | this calculation. 6 | 7 | Ruby: def get_volume_of_cuboid(length, width, height) 8 | 9 | =end 10 | 11 | # My Solution 12 | def get_volume_of_cuboid(l, w, h) 13 | l * w * h 14 | end 15 | 16 | # Another Solution 17 | def get_volume_of_cuboid(*dimensions) 18 | dimensions.inject(:*) 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/8KYU/VowelInThere.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Given an array of numbers (s), check if any of the numbers are the character 4 | codes for lower case vowels. 5 | 6 | If they are, change the array value to a string of that vowel. 7 | 8 | Return the resulting array. 9 | 10 | =end 11 | 12 | # My Solution 13 | def is_vow(a) 14 | a.each_with_index do |x,i| 15 | letter = "" 16 | case x 17 | when 97 ; letter = "a" 18 | when 101 ; letter = "e" 19 | when 105 ; letter = "i" 20 | when 111 ; letter = "o" 21 | when 117 ; letter = "u" 22 | end 23 | a[i] = letter if letter != "" 24 | end 25 | end 26 | 27 | # Better Solution 28 | def is_vow(a) 29 | a.map { |l| l.chr =~ /[aeoui]/ ? l.chr : l } 30 | end 31 | -------------------------------------------------------------------------------- /Ruby/8KYU/VowelRemove.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function called shortcut to remove all the lowercase vowels in a given 4 | string. 5 | 6 | Examples 7 | 8 | shortcut("codewars") # --> cdwrs 9 | shortcut("goodbye") # --> gdby 10 | Don't worry about uppercase vowels. 11 | 12 | =end 13 | 14 | # My Solution 15 | def shortcut(s) 16 | s.tr('aeiou','') 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/8KYU/WelcomeCity.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a method say_hello that takes as input a name, city, and state to welcome 4 | a person. Note that name will be an array consisting of one or more values that 5 | should be joined together with one space betweeen each, and the length of the 6 | name array in test cases will vary. 7 | 8 | Example 9 | 10 | say_hello(['John', 'Smith'], 'Phoenix', 'Arizona') 11 | This example returns the string Hello, John Smith! Welcome to Phoenix, Arizona! 12 | 13 | =end 14 | 15 | # My Solution 16 | def say_hello(name, city, state) 17 | "Hello, #{name.each{|x|}.join(" ")}! Welcome to #{city}, #{state}!" 18 | end 19 | 20 | # Better Solution 21 | def say_hello(name, city, state) 22 | "Hello, #{name.join(' ')}! Welcome to #{city}, #{state}!" 23 | end 24 | -------------------------------------------------------------------------------- /Ruby/8KYU/WellofIdeas.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | For every good kata idea there seem to be quite a few bad ones! 4 | 5 | In this kata you need to check the provided array (x) 6 | for good ideas 'good' and bad ideas 'bad'. If there are 7 | one or two good ideas, return 'Publish!', if there are 8 | more than 2 return 'I smell a series!'. If there are no 9 | good ideas, as is often the case, return 'Fail!'. 10 | 11 | =end 12 | 13 | # My solution 14 | def well(x) 15 | good = 0 16 | x.each do |a| 17 | good += 1 if a == 'good' 18 | end 19 | if good > 2 20 | return 'I smell a series!' 21 | elsif good > 0 22 | return 'Publish!' 23 | else 24 | return 'Fail!' 25 | end 26 | end 27 | 28 | # Better solution 29 | def well(x) 30 | goods = x.count('good') 31 | goods > 2 ? 'I smell a series!' : goods > 0 ? 'Publish!' : 'Fail!' 32 | end 33 | -------------------------------------------------------------------------------- /Ruby/8KYU/YieldBlock.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the Compute method/function so that if a block is given it 4 | will run else it returns "Do not compute". 5 | 6 | =end 7 | 8 | # My solution 9 | def compute (&block) 10 | return "Do not compute" if block == nil 11 | block.call 12 | end 13 | 14 | # Better solution 15 | def compute 16 | block_given? ? yield : "Do not compute" 17 | end 18 | 19 | # Another solution 20 | def compute 21 | yield rescue "Do not compute" 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/Authored/4KYU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/4KYU.png -------------------------------------------------------------------------------- /Ruby/Authored/5KYU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/5KYU.png -------------------------------------------------------------------------------- /Ruby/Authored/6KYU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/6KYU.png -------------------------------------------------------------------------------- /Ruby/Authored/7KYU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/7KYU.png -------------------------------------------------------------------------------- /Ruby/Authored/8KYU.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/8KYU.png -------------------------------------------------------------------------------- /Ruby/Authored/Beta.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/Beta.png -------------------------------------------------------------------------------- /Ruby/Authored/Fish.rb: -------------------------------------------------------------------------------- 1 | def fish(shoal) 2 | level = 1 3 | total = 0 4 | fishes = shoal.split("").sort 5 | fishes.each do |fish| 6 | if fish.to_i <= level 7 | total += (1*fish.to_i) 8 | if total >= (level*4) 9 | total -= (level*4) 10 | level += 1 11 | end 12 | end 13 | end 14 | level 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/Authored/Fruit.rb: -------------------------------------------------------------------------------- 1 | def fruit(reels, spins) 2 | reel = ["Wild","Star","Bell","Shell","Seven","Cherry","Bar","King","Queen","Jack"] 3 | items = [reels[0][spins[0]], reels[1][spins[1]], reels[2][spins[2]]] 4 | return (10 - reel.index(items[0])) * 10 if items[0] == items[1] && items[0] == items[2] 5 | item = nil; extra = nil 6 | (item = items[0]; extra = items[2]) if items[0] == items[1] 7 | (item = items[0]; extra = items[1]) if items[0] == items[2] 8 | (item = items[1]; extra = items[0]) if items[1] == items[2] 9 | if item != nil 10 | num = 10 - reel.index(item) 11 | num = num * 2 if extra == "Wild" 12 | return num 13 | end 14 | return 0 15 | end 16 | -------------------------------------------------------------------------------- /Ruby/Authored/Square1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/Square1.png -------------------------------------------------------------------------------- /Ruby/Authored/Square2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/Square2.png -------------------------------------------------------------------------------- /Ruby/Authored/Square3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/Square3.png -------------------------------------------------------------------------------- /Ruby/Authored/SquareExample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/SquareExample.png -------------------------------------------------------------------------------- /Ruby/Authored/Wave.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/Wave.png -------------------------------------------------------------------------------- /Ruby/Authored/Wave.rb: -------------------------------------------------------------------------------- 1 | def wave(str) 2 | result = [] 3 | str = str.split("") 4 | str.each_with_index do |a,b| 5 | if str[b] != " " 6 | temp = [] 7 | str.each_with_index{|x,i| i == b ? temp << x.upcase : temp << x} 8 | result << temp.join 9 | end 10 | end 11 | result 12 | end 13 | 14 | letters = "abcd efghi jklmno pqrstu vwxyz".split("") 15 | srand 16 | 1.times do 17 | word = [] 18 | (0..rand(0..200)).each do 19 | word << letters[rand(0..letters.length)] 20 | end 21 | word = word.join 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/Authored/atom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/atom.png -------------------------------------------------------------------------------- /Ruby/Authored/c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/c.png -------------------------------------------------------------------------------- /Ruby/Authored/carpark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/carpark.jpg -------------------------------------------------------------------------------- /Ruby/Authored/codewars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/codewars.png -------------------------------------------------------------------------------- /Ruby/Authored/connect4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/connect4.jpg -------------------------------------------------------------------------------- /Ruby/Authored/cpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/cpp.png -------------------------------------------------------------------------------- /Ruby/Authored/crystal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/crystal.png -------------------------------------------------------------------------------- /Ruby/Authored/csharp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/csharp.png -------------------------------------------------------------------------------- /Ruby/Authored/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/demo.png -------------------------------------------------------------------------------- /Ruby/Authored/drivinglicense.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/drivinglicense.jpg -------------------------------------------------------------------------------- /Ruby/Authored/drunk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/drunk.png -------------------------------------------------------------------------------- /Ruby/Authored/fish.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/fish.jpg -------------------------------------------------------------------------------- /Ruby/Authored/fruit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/fruit.jpg -------------------------------------------------------------------------------- /Ruby/Authored/fsharp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/fsharp.png -------------------------------------------------------------------------------- /Ruby/Authored/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/github.png -------------------------------------------------------------------------------- /Ruby/Authored/guesswho.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/guesswho.png -------------------------------------------------------------------------------- /Ruby/Authored/guesswho_banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/guesswho_banner.jpg -------------------------------------------------------------------------------- /Ruby/Authored/haskell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/haskell.png -------------------------------------------------------------------------------- /Ruby/Authored/heroku.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/heroku.png -------------------------------------------------------------------------------- /Ruby/Authored/hippo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/hippo.jpg -------------------------------------------------------------------------------- /Ruby/Authored/hippo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/hippo.png -------------------------------------------------------------------------------- /Ruby/Authored/java.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/java.png -------------------------------------------------------------------------------- /Ruby/Authored/javascript.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/javascript.png -------------------------------------------------------------------------------- /Ruby/Authored/mastermind.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/mastermind.jpg -------------------------------------------------------------------------------- /Ruby/Authored/mastermind1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/mastermind1.png -------------------------------------------------------------------------------- /Ruby/Authored/mastermind2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/mastermind2.png -------------------------------------------------------------------------------- /Ruby/Authored/mazerunner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/mazerunner.jpg -------------------------------------------------------------------------------- /Ruby/Authored/php.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/php.png -------------------------------------------------------------------------------- /Ruby/Authored/python.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/python.png -------------------------------------------------------------------------------- /Ruby/Authored/ruby.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/ruby.png -------------------------------------------------------------------------------- /Ruby/Authored/scoobydoopuzzle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/scoobydoopuzzle.jpg -------------------------------------------------------------------------------- /Ruby/Authored/snakesandladders.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/snakesandladders.jpg -------------------------------------------------------------------------------- /Ruby/Authored/snakesandladdersboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/snakesandladdersboard.jpg -------------------------------------------------------------------------------- /Ruby/Authored/sql.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/sql.png -------------------------------------------------------------------------------- /Ruby/Authored/squares_banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/squares_banner.jpg -------------------------------------------------------------------------------- /Ruby/Authored/typescript.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/typescript.png -------------------------------------------------------------------------------- /Ruby/Authored/vendingmachine.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrianeyre/codewars/350db3f3addd3f601d42773f19cbb9cefefbc575/Ruby/Authored/vendingmachine.jpg -------------------------------------------------------------------------------- /Ruby/Translation/AllStar02.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function, named findAverage, that takes an array of ages of All Star 4 | Code students and returns the average (do NOT round the return value). 5 | 6 | allStars = [17,16,16,16,16,15,17,17,15,5,17,17,16] 7 | findAverage(allStars) # => 15.384615384615385 8 | 9 | =end 10 | 11 | # My Solution 12 | def findAverage(values) 13 | values.inject(&:+).to_f/values.length 14 | end 15 | 16 | =begin 17 | # Random tests for code wars 18 | (1..200).each do |rtest| 19 | allStars = [] 20 | (2..30).each {|x| allStars << rand(1..99)} 21 | solution = findAverage(allStars) 22 | Test.assert_equals(findAverage(allStars),solution,"Expected: '#{solution}'") 23 | end 24 | =end 25 | -------------------------------------------------------------------------------- /Ruby/Translation/AllStar03.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function, called removeVowels, that takes a string argument and returns 4 | that same string with all vowels removed (vowels are "a", "e", "i", "o", "u"). 5 | 6 | removeVowels("drake") # => "drk" 7 | removeVowels("aeiou") # => "" 8 | 9 | =end 10 | 11 | # My Solution 12 | def removeVowels(word) 13 | word.split("").reject {|x| x =~ /[aeiou]/}.join 14 | end 15 | 16 | =begin 17 | # Codewars random tests 18 | letters = "abcdefghijklmnopqrstuvwxyz" 19 | letters = letters.split("") 20 | (1..200).each do |rtest| 21 | word = [] 22 | (2..100).each {|x| word << letters[rand(0..25)]} 23 | word = word.join 24 | solution = removeVowels2(word) 25 | Test.assert_equals(removeVowels(word),solution,"Expected: '#{solution}'") 26 | end 27 | =end 28 | -------------------------------------------------------------------------------- /Ruby/Translation/AllStar05.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function, called randomMovies, that takes in an array of movie strings 4 | and returns one of those movies randomly 5 | 6 | movies = ["Rocky", "Rocky II", "Rocky III", "Rocky IV"] 7 | randomMovies(movies) # => "Rocky" 8 | 9 | =end 10 | 11 | # My Solution 12 | def randomMovies(movies) 13 | movies[rand(movies.length)] 14 | end 15 | 16 | (1..200).each do |rtest| 17 | Test.assert_equals(randomMovies(movies), true) 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/Translation/CountZeros.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create an algorithm to count the number of zeros that appear between 1 and N. 4 | 5 | Examples: 6 | 7 | There are 2 zeros from 1 to 20: 10, 20 8 | 9 | There are 11 zeros from 1 to 100: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 10 | 11 | countZeros(10); // returns 1 12 | countZeros(100); // returns 11 13 | countZeros(200); // returns 31 14 | 15 | =end 16 | 17 | # My Solution 18 | def countZeros2(n) 19 | result = 0 20 | (1..n).each {|x| result += x.to_s.count("0")} 21 | result 22 | end 23 | 24 | =begin 25 | # Codewars random tests 26 | (1..200).each do |rtest| 27 | num = rand(1..200) 28 | solution = countZeros2(num) 29 | Test.assert_equals(countZeros(num),solution,"Expected: '#{solution}'") 30 | end 31 | =end 32 | -------------------------------------------------------------------------------- /Ruby/Translation/SeriesInt.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function generateIntegers/generate_integers that accepts a single 4 | argument n/$n and generates an array containing the integers from 0 to n/$n 5 | inclusive. 6 | 7 | For example, generateIntegers(3)/generate_integers(3) should return [0, 1, 2, 3] 8 | 9 | n/$n can be any integer greater than or equal to 0. 10 | 11 | =end 12 | def generateIntegers2(n) 13 | (0..n).each.map {|x| x} 14 | end 15 | 16 | =begin 17 | 18 | # Codewars random tests 19 | (1..200).each do |rtest| 20 | num = rand(0..200) 21 | solution = generateIntegers2(num) 22 | Test.assert_equals(generateIntegers(num),solution,"Expected: '#{solution}'") 23 | end 24 | =end 25 | -------------------------------------------------------------------------------- /Ruby/Translation/SeriesMtoN.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function generateIntegers/generate_integers that accepts two arguments 4 | m/$m and n/$n and generates an array containing the integers from m to n inclusive. 5 | 6 | For example, generateIntegers(2, 5)/generate_integers(2, 5) should return [2, 3, 4, 5]. 7 | 8 | m/$m and n/$n can be any integers greater than or equal to 0. 9 | 10 | n/$n will always be greater than or equal to m/$m. 11 | 12 | =end 13 | 14 | # My Solution 15 | def generateIntegers(m,n) 16 | (m..n).each.map {|x| x} 17 | end 18 | 19 | =begin 20 | # Codewars random tests 21 | (1..200).each do |rtest| 22 | m = rand(0..100) 23 | n = rand(m+1..200) 24 | solution = generateIntegers2(m,n) 25 | Test.assert_equals(generateIntegers(m,n),solution,"Expected: '#{solution}'") 26 | end 27 | =end 28 | -------------------------------------------------------------------------------- /Ruby/Translation/Switch.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function caseInSwitch which uses a switch statement to test the variable 4 | val and sets the variable answer according to the following values of 5 | val: 1 - "alpha" 2 - "beta" 3 - "gamma" 4 - "delta" 6 | 7 | Then, test caseInSwitch for the argument 1 and print the value of answer to the 8 | console. 9 | 10 | =end 11 | 12 | # My Solution 13 | def caseInSwitch(n) 14 | case n 15 | when 1 ; "alpha" 16 | when 2 ; "beta" 17 | when 3 ; "gamma" 18 | when 4 ; "delta" 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/Translation/WordCount.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that counts all words in a string 4 | 5 | =end 6 | 7 | # My Solution 8 | def countWords(str) 9 | str.split(" ").length 10 | end 11 | 12 | =begin 13 | # Codewars random tests 14 | words = ["racecar","madam","kitty","wolf","robert trebor","kata","code wars", 15 | "code edoc","anna","level","radar","sagas","man","woman","internet","website", 16 | "yes","no","this","is","another","word","in","the","random","array","of","word"] 17 | 18 | (1..200).each do |rtest| 19 | word = [] 20 | (1..rand(100)).each {|x| word << words[rand(words.length)]} 21 | word = word.join(" ") 22 | solution = countWords2(word) 23 | Test.assert_equals(countWords(word),solution,"Expected: '#{solution}'") 24 | end 25 | =end 26 | -------------------------------------------------------------------------------- /Ruby/beta/123.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your team is writing a fancy new text editor and you've been tasked with 4 | implementing the line numbering. 5 | 6 | Write a function which takes a list of strings and returns each line prepended 7 | by the correct number. 8 | 9 | The numbering starts at 1. The format is n: string. Notice the colon and space 10 | in between. 11 | 12 | Examples: 13 | 14 | number [] # => [] 15 | number ["a", "b", "c"] # => ["1: a", "2: b", "3: c"] 16 | 17 | =end 18 | 19 | # My Solution 20 | def number lines 21 | result = [] 22 | lines.each_with_index {|x,i| result << "#{i+1}: #{x}"} 23 | result.flatten 24 | end 25 | 26 | # Better Solution 27 | def number lines 28 | lines.map.with_index(1) { |l, i| "#{i}: #{l}" } 29 | end 30 | -------------------------------------------------------------------------------- /Ruby/beta/AllStar16.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function called noRepeat() that takes a string argument and returns a 4 | single letter string of the first not repeated character in the entire string. 5 | 6 | noRepeat("aabbccdde") // => "e" 7 | noRepeat("wxyz") // => "w" 8 | noRepeat("testing") // => "e" 9 | Note: ONLY letters from the english alphabet will be used as input There will 10 | ALWAYS be at least one non-repeating letter in the input string 11 | 12 | =end 13 | # My Solution 14 | def no_repeat(string) 15 | string.split("").each{|x| return x if string.count(x) == 1} 16 | end 17 | 18 | # Better Solution 19 | def no_repeat(string) 20 | string.chars.find{ |c| string.count(c) == 1 } 21 | end 22 | -------------------------------------------------------------------------------- /Ruby/beta/Bath.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Apparently, in the US, January is National Bath Safety Month!! 4 | 5 | To celebrate, in this kata you need to decide if a string (s) is 6 | safe from falling in the bath (x)! 7 | 8 | Very basic, if the string can fit in the bath, it could fall in. 9 | We can not allow that in safety month! If the string can fit in, it's not safe so return false, otherwise return true, as the string couldn't fit in if it wanted to! 10 | 11 | Remember the sides of the bath do not count as water! 12 | 13 | =end 14 | 15 | # My Soltuion 16 | def bath(s,x) 17 | s.length >= (x.length - 2) ? true : false 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/beta/CamelCase.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write simple .camelcase method for strings. All words must have their first 4 | letter capitalized without spaces. 5 | 6 | For instance: 7 | 8 | 'hello case'.camelcase => HelloCase 9 | 'camel case word'.camelcase => CamelCaseWord 10 | 11 | =end 12 | 13 | # My Solution 14 | class String 15 | def camelcase 16 | self.split(" ").map {|x| x.capitalize}.join 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/beta/ConvertSymArray.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Many classes in the Ruby core library offer methods to convert from 4 | one data type to another 5 | 6 | Add a .to_ary method to the Symbol class to convert it to an 1 length array. 7 | 8 | For example 9 | 10 | :a.to_ary #=> [:a] 11 | 12 | =end 13 | 14 | # My Solution 15 | class Symbol 16 | def to_ary 17 | [self] 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /Ruby/beta/CountLetter.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | In this kata, you've to count letters in a given string and return the 4 | letter count in a hash with 'letter' as key and count as 'value'. 5 | The key must be 'symbol' instead of string in Ruby and 'char' instead 6 | of string in Crystal. 7 | 8 | Example: 9 | 10 | letter_count('arithmetics') #=> {:a=>1, :c=>1, :e=>1, :h=>1, :i=>2, :m=>1, 11 | :r=>1, :s=>1, :t=>2} 12 | 13 | =end 14 | 15 | # My Solution 16 | def letter_count(str) 17 | letter = Hash.new {|x,k| x[k] = 0} 18 | str.split("").each {|x| letter[x.to_sym] += 1} 19 | letter 20 | end 21 | 22 | # Better Soltuion 23 | def letter_count(str) 24 | str.chars.each_with_object(Hash.new(0)) { |c, h| h[c.to_sym] += 1 } 25 | end 26 | 27 | # Another Solution 28 | def letter_count(str) 29 | str.chars.map { | i | [:"#{i}", str.count(i)] }.to_h 30 | end 31 | -------------------------------------------------------------------------------- /Ruby/beta/FindArray.rb: -------------------------------------------------------------------------------- 1 | def find(string, array) 2 | array.include?(string) 3 | end -------------------------------------------------------------------------------- /Ruby/beta/Friday.rb: -------------------------------------------------------------------------------- 1 | def killcount(counselors, jason) 2 | counselors.select{|x| x[1] < jason}.map{|x| x[0]} 3 | end -------------------------------------------------------------------------------- /Ruby/beta/Golden.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a method golden? that takes two numeric arguments x, y which represent 4 | dimensions of a 2D rectangle. Return true if the numbers adhere to the golden 5 | ratio and false otherwise. 6 | 7 | =end 8 | 9 | # My Solution 10 | def golden?(x, y) 11 | return false if x == y 12 | (y/(x-y)).round(2) == (x/y).round(2) ? true : false 13 | end 14 | -------------------------------------------------------------------------------- /Ruby/beta/HelloWorld.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Complete the function body for this hello() function so that it takes one 4 | argument (person, a string) and returns a string saying hello to that person. 5 | The result should be formatted so that when you call the function like this: 6 | 7 | hello('Grae') 8 | you return a string that looks like this: 9 | 10 | 'Hello, Grae' 11 | 12 | =end 13 | 14 | # My Solution 15 | def hello(name) 16 | "Hello, #{name}" 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/beta/Isogram.rb: -------------------------------------------------------------------------------- 1 | def isogram?(s) 2 | return false if s.is_a? Integer 3 | return false if s==nil || s =="" 4 | num = s.count(s[0]) 5 | s.split("").each {|x| return false if s.count(x) != num} 6 | true 7 | end -------------------------------------------------------------------------------- /Ruby/beta/KeepHoop.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Alex just got a new hula hoop, he loves it but feels discouraged because his 4 | little brother is better than him 5 | 6 | Write a program where Alex can input (n) how many times the hoop goes round and 7 | it will return him an encouraging message :) 8 | 9 | -If Alex gets 10 or more hoops, return the string "Great, now move on to tricks" 10 | 11 | -If he doesn't get 10 hoops, return the string "Keep at it until you get it". 12 | 13 | =end 14 | 15 | # My Solution 16 | def hoopCount n 17 | n >= 10 ? "Great, now move on to tricks" : "Keep at it until you get it" 18 | end 19 | -------------------------------------------------------------------------------- /Ruby/beta/NdivXandY.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a function isDivisible(n,x,y) that checks if a number n is divisible by 4 | two numbers x AND y 5 | 6 | Example: 7 | is_divisible(3,1,3)--> true because 3 is divisible by 1 and 3 8 | is_divisible(12,2,6)--> true because 12 is divisible by 2 and 6 9 | is_divisible(100,5,3)--> false because 100 is not divisible by 3 10 | is_divisible(12,7,5)--> false because 12 is neither divisible by 7 nor 5 11 | 12 | =end 13 | 14 | # My Solution 15 | def is_divisible(n,x,y) 16 | n % x == 0 && n % y == 0 ? true : false 17 | end 18 | 19 | # Another Solution 20 | def is_divisible(n, *divisors) 21 | divisors.all? { |div| n % div == 0 } 22 | end 23 | -------------------------------------------------------------------------------- /Ruby/beta/Pirate.rb: -------------------------------------------------------------------------------- 1 | # The Spanish King wants to bring gold back to the empire. 2 | # The first step is finding pirates and the gold in the new world. 3 | 4 | # My Solution 5 | def secret_map(sp) 6 | "count of pirates: #{sp.flatten.count("pirate")} and the count of gold piles: #{sp.flatten.count("pile_of_gold")}" 7 | end 8 | -------------------------------------------------------------------------------- /Ruby/beta/RobotWalk.rb: -------------------------------------------------------------------------------- 1 | def walk_home?(str) 2 | x = 0 ; y = 0 3 | str.split("").each do |x| 4 | case x 5 | when "n" ; y -= 1 6 | when "e" ; x += 1 7 | when "s" ; y += 1 8 | when "w" ; x -= 1 9 | end 10 | end 11 | x == 0 && y == 0 ? true : false 12 | end 13 | 14 | p walk_home?("nenessww") 15 | -------------------------------------------------------------------------------- /Ruby/beta/SchoolPaperword.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Your classmates asked you to copy some paperwork for them. You know that there 4 | are 'n' classmates and the paperwork has 'm' pages. 5 | 6 | Your task is to calculate how many blank pages do you need. 7 | 8 | Example: 9 | paperwork(5, 5) == 25 10 | Note! if n or m < 0 return 0! 11 | 12 | =end 13 | 14 | # My Solution 15 | def paperwork(n, m) 16 | n < 0 || m < 0 ? 0 : n*m 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/beta/SevenAte9.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Write a function that removes each 9 that it is in between 7s. 4 | 5 | seven_ate9('79712312') => '7712312' 6 | seven_ate9('79797') => '777' 7 | Input: String Output: String 8 | 9 | =end 10 | 11 | # My Solution 12 | def seven_ate9(str) 13 | (0..5).each {|x| str = str.gsub(/797/,"77")} 14 | str 15 | end 16 | 17 | # Better Solution 18 | def seven_ate9(str) 19 | str.gsub(/(?<=7)9(?=7)/,'') 20 | end 21 | -------------------------------------------------------------------------------- /Ruby/beta/TerminalCombat.rb: -------------------------------------------------------------------------------- 1 | =begin 2 | 3 | Create a combat function that takes the player's current health and the amount 4 | of damage recieved, and returns the player's new health. Health can't be less 5 | than 0. 6 | 7 | =end 8 | 9 | # My Solution 10 | def combat(health, damage) 11 | health - damage > 0 ? health - damage : 0 12 | end 13 | 14 | # Better Solution 15 | def combat(health, damage) 16 | [health-damage,0].max 17 | end 18 | -------------------------------------------------------------------------------- /Ruby/beta/YesNo.rb: -------------------------------------------------------------------------------- 1 | def yes_no(arr) 2 | result = [] 3 | p arr 4 | loop do 5 | new_result = [] 6 | if arr.length != 2 7 | arr.each_with_index {|x,i| new_result << x if i.even?} 8 | else 9 | arr.each_with_index {|x,i| new_result << x if i.odd?} 10 | end 11 | #arr.each {|x| result << x if x.even?} 12 | arr = arr - new_result 13 | 14 | p arr 15 | #arr.each {|x| new_result << x if x.odd?} 16 | result << new_result 17 | break if arr.length < 1 18 | #p result.flatten 19 | end 20 | result.flatten 21 | end 22 | 23 | p yes_no(['this', 'code', 'is', 'right', 'the']) 24 | -------------------------------------------------------------------------------- /SQL/8KYU/Active.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM students WHERE IsActive; -------------------------------------------------------------------------------- /SQL/8KYU/Distinct.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT age FROM people; -------------------------------------------------------------------------------- /SQL/8KYU/Grasshopper.sql: -------------------------------------------------------------------------------- 1 | SELECT custid, custname, custstate FROM customers; -------------------------------------------------------------------------------- /SQL/8KYU/Order.sql: -------------------------------------------------------------------------------- 1 | SELECT * from people WHERE age > 50 ORDER BY age DESC; -------------------------------------------------------------------------------- /SQL/BETA/Adults.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM users WHERE age > 17; --------------------------------------------------------------------------------