Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Pesho is given N sets of numbers, and he has to count the odd numbers in each set, and then compare them.
- The number C shows how many numbers are there in a set. Then you are given a string S holding one of the words
- "odd" or "even" showing you what numbers should be counted. Then you are given N * C numbers representing all sets.
- Your task is to count the odd or even numbers in each set, and then say in which set has most S numbers.
- The set with most S numbers should be represented as ordinal number word e.g. "First", "Second", "Third",
- "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth".
- If the count of one or more sets is equal, print the first one with biggest count.
- If there is no set holding odd or even numbers print "No".
- The input consists of three input values, each at separate line:
- The first line holds an integer N – the count of sets.
- The second line hold an integer C – the count of numbers in each set.
- The third line holds a string S holding either "odd" or "even" showing what numbers to count.
- The output data should be printed on the console. It consists of exactly 1 line.
- Print on the console the following formatted string "{0} set has the most {1} numbers: {2}",
- where {0} is the set as ordinal string {1} is the value of S and {2} is the biggest count of S numbers.
- If there is no set holding odd or even numbers print "No". */
- "use strict";
- function solve(args) {
- var setLength = parseInt(args[1]);
- var targetType = args[2];
- var i;
- var setCounter = 0;
- var numsCounter = 0;
- var maxTypeCount = 0;
- var setMaxType = 0;
- var typeCounter = 0;
- for (i = 3; i < args.length; i += 1) {
- var number = parseInt(args[i]);
- var isOdd = number % 2 != 0;
- if ((isOdd && targetType == 'odd') || (!isOdd && targetType == 'even')) {
- typeCounter += 1;
- }
- numsCounter += 1;
- if (numsCounter == setLength) {
- setCounter += 1;
- if (typeCounter > maxTypeCount) {
- maxTypeCount = typeCounter;
- setMaxType = setCounter;
- if (maxTypeCount == setLength) break;
- }
- typeCounter = 0;
- numsCounter = 0;
- }
- }
- var setNames = [0, 'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth'];
- if (!maxTypeCount) {
- console.log('No');
- } else {
- console.log("%s set has the most %s numbers: %d", setNames[setMaxType], targetType, maxTypeCount);
- }
- }
- solve(['2', '5', 'odd', '6', '4', '12', '8', '199', '15', '21', '7', '3', '5']);
- solve(['3', '2', 'even', '1', '3', '5', '7', '9', '11']);
- solve(['3', '2', 'odd', '1', '3', '5', '9', '151', '193']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement