Tuesday 28 February 2017

Swap Case program in Java / Python


java
#############################################################
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
       
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        char [] tempArray = new char[str.length()];
        tempArray = str.toCharArray();

        for (int i = 0; i < str.length(); i++) {
            char c = tempArray[i];
            if(Character.isUpperCase(c)){
                tempArray[i] = Character.toLowerCase(c);
            }
            else if(Character.isLowerCase(c)){
                tempArray[i] = Character.toUpperCase(c);
            }
        }
       

        System.out.println(new String(tempArray));
    }
}

#########################################################

Python( Mind the indentation)
############################################################
def swap_case(s):
    st = ''
    for i in range(len(s)):
        if s[i].isupper():
            st += s[i].lower()
        else:
            st += s[i].upper()
    return st

if __name__ == '__main__':
    s = raw_input()
    result = swap_case(s)
    print result

##############################################################

Monday 27 February 2017

Akamai coding test question...

program to manipulate the string such that a sub-string given from a string should be replace by another given string. for ex:
sunday su mo => monday
ghosthouse house tree => ghosttree



code in python..

num = int(raw_input())
for i in range(num):
    s = raw_input().split(' ')
    print s[0].replace(s[1],s[2])

Sunday 26 February 2017

calculating all prime numbers till  given number...(Akamai technical coding test question-solution )

#include <iostream>
#include <vector>

using namespace std;

bool check(int j){
    for(int k = 2; k <= j/2; k++){
        if(j % k == 0){
            return false;
        }
        else {
            return true;
        }
    }
    return true;
}

void prime_num(int num){
    int count = 0;
    for(int j = 2; j < num; j++){
        if(check(j) == true){
            cout << j <<" ";
            count++;
            if(count == 5){
                count = 0;
                cout << endl;
            }
        }
    }
}


int main(){
    int n;
    cin >>n;
    if(n  < 2){cout << "no prime nos"; }
   
    else{
        prime_num(n);
    }
       
    return 0;
}

Tuesday 7 February 2017

Linked List insertion at the end and print

Linked List Implementation using pointers..



#include <iostream>

using namespace std;

struct Node
{
int data;
Node* next;
};

void Insert_linkedlist(Node** start, int x)
{
Node * temp = new Node();
temp->data = x;
temp->next = NULL;
if (*start != NULL)
{
Node *ptr = *start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
else
{
*start = temp;
}

}

void print_linkedlist(Node* start)
{
while (start != NULL)
{
cout << start->data << endl;
start = start->next;
}
}

int main()
{
Node* start = new Node();
    start = NULL;
int n, p;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> p;
Insert_linkedlist(&start, p);
}

print_linkedlist(start);

return 0;
}