Home30 Days Of CodeHackerrank Day 6 : Let's Review 30 days of code solution

Hackerrank Day 6 : Let’s Review 30 days of code solution

In this Hackerrank Day 6 : Let’s Review 30 days of code solution .

Task

Given a string, S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Note: 0 is considered to be an even index.

Input Format

The first line contains an integer, T(the number of test cases).
Each line i of the T subsequent lines contain a String, S.

Constraints

1<=T<=10
2<=length of S<=10000

Output Format

For each String Sj (where 0<=j<=T-1), print Sj‘s even-indexed characters, followed by a space, followed by Sj‘s odd-indexed characters.

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

Code Explanation

We first divide the string into even and odd number as we can see that in the string “Hacker“, ‘H’ is in even place and ‘a’ is in an odd place and so on. So our string is “Hacker” can be divided by even(H), odd(a), even(c), odd(k), even(e), and odd(r).

Step 1:- Run the first loop up to the size of the string and find the even character and print the even place character and print like below.

for(int i=0;i<s.size();i++)
{
 if(i%2==0)
 cout<<s[i];
}

after the first loop complete print space so we can print and separate the second loop.

cout<<" ";

Step 2:- Run the second loop up to the size of the string and find the odd place character and print the odd place character and print like below.

for(int i=0;i<s.size();i++)
{
 if(i%2!=0)
 cout<<s[i];
}

At the end print the new line for the next input to the early and next input print in the new line. find below let’s review hackerrank solution.

cout<<endl;

Hackerrank Day 6 : Let’s Review 30 days of code solution

Let’s Review HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n,i; // n number of arrays,

    scanf("%d",&n);
    // malloc an array of n char array pointers.
    char **array = (char**) malloc(sizeof(char *)*n);

    // allocate memory for each of the char array pointers
    for(i=0;i<n;i++) {
        array[i] = malloc(sizeof(char)*20000);
        scanf("%s",array[i]);
    }

    for(i = 0; i < n; i++) {
        int end = strlen(array[i]) - 2;
        int funnyFlag = 1;
        
        for(int j = 1; j < strlen(array[i]) - 1; j++) {       
            
            if(abs(array[i][j] - array[i][j - 1]) != abs(array[i][end] - array[i][end+1])) {                  
                funnyFlag = 0;                
                break;
            }     
            
            end--;              
        }
        
        if(funnyFlag == 1){
            printf("Funny\n");
        }else {
            printf("Not Funny\n");
        }
        free(array[i]);
    }

    free(array);
    return 0;
}

Let’s Review HackerRank Solution in C ++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int T;
    int test;
    int N;
    cin>>T;
    string S;
    for(int j=0;j<T;j++){
        cin>>S;
        test=1;
        N=S.size();
        for(int i=1;i<N;i++){
            if(abs(S[i]-S[i-1])!=abs(S[N-i]-S[N-i-1])){
                test=0;
                break;
            }
        }
        if(test==0){
            cout<<"Not Funny"<<endl;
        } else{
            cout<<"Funny"<<endl;
        }
        
    }
    return 0;
}

Let’s Review HackerRank Solution in Java

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for(int i=0;i<n;i++){
            String s = sc.nextLine();
            String r = new StringBuilder(s).reverse().toString();
            char sArr[] = s.toCharArray();
            char rArr[] = r.toCharArray();
            int j = 1;
            int k = s.length();
            int flag = 1;
            while(j < k){
                if(Math.abs(sArr[j]-sArr[j-1]) != Math.abs(rArr[j]-rArr[j-1])){
                    System.out.println("Not Funny");
                    flag = 0;
                    break;
                }
                ++j;
            }
            if(flag == 1){
            System.out.println("Funny");
            }
        }
    }
}

Let’s Review HackerRank Solution in Python 3

# Enter your code here. Read input from STDIN. Print output to STDOUT

n = int(input())
temp = []
for i in range(0,n):
    s = input()
    temp.append(s)

for i in range(0,n):
    for j in range(0,len(temp[i]),2):
        print(temp[i][j],end='')
    print(end=' ')
    for j in range(1,len(temp[i]),2):
        print(temp[i][j],end='')
    print()

Let’s Review HackerRank Solution in JavaScript

function funnyOrNot(word) {
    var sval;
    var rval;
    var j = word.length - 1;
    
    for (var i = 1; i < word.length; i++) {
        sval = Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1));
        rval = Math.abs(word.charCodeAt(j) - word.charCodeAt(j - 1));
        //process.stdout.write(sval + " " + rval + "\n");
        if (sval !== rval) {
            process.stdout.write("Not ");
            break;
        }
        j--;
    }
    
    process.stdout.write("Funny\n");
}

function processData(input) {
    //Enter your code here
    var pieces = input.split('\n');
    for (var i = 0; i < parseInt(pieces[0]); i++) {
        funnyOrNot(pieces[i+1]);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

NEXT : Hackerrank Day 7 : Arrays 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29


Read More –

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories