Join Regular Classroom : Visit ClassroomTech

New Coding Program | Set-5 SOLVED | codewindow.in

code program

Program 1. FindAsciSumOddEven:
Write code for function findAsciSumOddEven that takes one String as input. Input 1 is a sentence where words are separated by space. Now, find the sum of ASCII for each individual word in that string. Then, find whether this sum is odd or even. Finally, return the total number of odd sums and even sums available in the following format: “odd” + oddcount + “even” + evencount.

Function Prototype:
def findAsciSumOddEven (input1):
Write your code

Assumption:
1) In the given input sentence, Words are separated by exactly one space.
2) Given input sentence will not be an empty string.

Note:
1) Ascii value of ‘a’ is 97. Ascii value of ‘b’ is 98 and so on.
2) Ascii value of ‘A’ is 65. Ascii value of ‘B’ is 66 and so on.
3) The function is expected to return a String s answer. Just printing the answer in output is not needed. You need to return the answer.
4) Do not read the input data from the keyboard. it will be supplied as an argument to the function.

Example 1

Input:
a b c

Output:
odd2even1

Explanation:
There are three words in input1
Consider the first word: a. Sum of ASCII = 97. It is an odd value.
Consider the second word: b. Sum of ASCII = 98. It is an even value.
Consider the third word: c. Sum of ASCII = 99. It is an odd value. So totally, 2 odd and 1 even ASCII sums are there. Hence, return ‘odd2even1’ as answer.

Example 2

Input:
aaa bb cc

Output:
odd1even2

Explanation:

There are three words in input1.
Consider the first word: aaa. Sum of ASCII = 97*3 = 291. It is an odd value.
Consider the second word: bb. Sum of ASCII = 98*2 = 196. It is an even value.
Consider the third word: cc. Sum of ASCII = 99*2 = 198. It is an even value. So, totally, 1 odd and 2 even ASCII sums are there. Hence, return ‘odd1even2’ as answer.

Solution: In Java

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

class CodeWindow
{
    static void findAsciSumOddEven(String[] str) {
        int odd = 0, even = 0;
        for(int i=0; i<str.length; i++) {
            int total = 0;
            for(int j=0; j<str[i].length(); j++) {
                total += str[i].charAt(j);
            }
            if(total%2 == 0)
                even++;
            else
                odd++;
        }
        System.out.println("odd" + odd + "even" + even);
    }
    
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().split(" ");
		findAsciSumOddEven(str);
	}
}

Program 2. Write a program that will take 5 numbers as input (one number per line). The program will then print the cumulative sums of the numbers as output.

Example 1

Input:
1
2
3
4
5

Output:
1
3
6
10
15

Solution: In Java

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

class CodeWindow
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int total = 0;
		for(int i=0; i<5; i++) {
		    int val = Integer.parseInt(br.readLine());
		    total += val;
		    System.out.println(total);
		}
	}
}

Program 3. Phone number

When we share our phone numbers with others, we share the numbers digit by digit.
Given a phone number in words, convert the number to digits.
For example, convert “Six four eight three to “6483”.
1. All phone numbers will be 10 digits.
2. Two repeating digits will be shortened using the word “double”. Three repeating digits will be shortened using the word “triple”. If the digits repeat four or more times, they will be shortened using “double” and “triple” multiple times.
3. The input will be always valid. For example, we won’t have a scenario like “double double two”. It will be “double two double two”
4. The entire input will be in lowercase.

Example 1

Input:
two one nine six eight one six four six zero

Output:
2196816460

Solution: In Java

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

class CodeWindow
{
    static char returnDig(String n) {
        if(n.equals("zero")) return '0';
		else if(n.equals("one")) return '1';
		else if(n.equals("two")) return '2';
		else if(n.equals("three")) return '3';
		else if(n.equals("four")) return '4';
		else if(n.equals("five")) return '5';
		else if(n.equals("six")) return '6';
		else if(n.equals("seven")) return '7';
		else if(n.equals("eight")) return '8';
		return '9';
    }
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().split(" ");
		String number = "";
		boolean d = false, t = false;
		for(int i=0; i<str.length; i++) {
		    if(d == true) {
		        number += "" + returnDig(str[i]) + returnDig(str[i]);
		        d = false;
		    }
		    else if(t == true) {
		        number +=  "" + returnDig(str[i]) + returnDig(str[i]) + returnDig(str[i]);
		        t = false;
		    } else {
    		    if(str[i].equals("double")) d = true;
    		    else if(str[i].equals("triple")) t = true;
    		    else number += returnDig(str[i]);
		    }
		}
		System.out.println(number);
	}
}
JECA Questions

Also Checkout

Recent Posts
Categories
Pages