Friday, May 27, 2016

C++ solved inheritance problem from hackerrank

C++ solved inheritance problem from hackerrank:

#include <iostream>
#include <vector>

using namespace std;


class Person{
    protected:
        string firstName;
        string lastName;
        int id;
    public:
        Person(string firstName, string lastName, int identification){
            this->firstName = firstName;
            this->lastName = lastName;
            this->id = identification;
        }
        void printPerson(){
            cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n";
        }
   
};

class Student :  public Person{
    private:
        vector<int> testScores; 
    public:
          // Write your constructor
        Student(string firstName, string lastName, int identification, vector<int> scores): Person(firstName, lastName, identification), testScores(scores) {
        }
          // Write char calculate()
        char calculate() {
            int sum = 0;
            int count = 0;
            vector<int>::iterator it;
            for(it = testScores.begin(); it != testScores.end(); ++it) {
                sum+= *it;
                count++;
            }
            int avg = sum/count;
            if (90 <= avg && avg <=100)
                return 'O';
            else if(80<= avg && avg < 90)
                return 'E';
            else if(70<= avg && avg < 80)
                return 'A';
            else if(55<= avg && avg < 70)
                return 'P';
            else if(40 <= avg && avg < 55)
                return 'D';
             else
                return 'T';
        }
};

int main() {
    string firstName;
      string lastName;
    int id;
      int numScores;
    cin >> firstName >> lastName >> id >> numScores;
      vector<int> scores;
      for(int i = 0; i < numScores; i++){
          int tmpScore;
          cin >> tmpScore;
        scores.push_back(tmpScore);
    }
    Student* s = new Student(firstName, lastName, id, scores);
    s->printPerson();
    cout << "Grade: " << s->calculate() << "\n";
    return 0;
}

Sunday, May 8, 2016

progrom for converting time from 12 hr format to 24 hr format


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    char* time = (char *)malloc(10240 * sizeof(char));
    scanf("%s",time);
    int count = 0;
   
    int hh = (time[0] - '0') * 10 + (time[1] - '0');
    for(int i=0;i < 10240;i++) {
        if ((time[i] == 'P' || time[i] == 'p') && (time[i+1] == 'M' || time[i+1] =='m')) {
            if (hh > 11) break;
            if (hh == 24 )
                hh = 0;
            else
                hh+=12;
            break;
        }
        if ((time[i] == 'A'  || time[i+1] =='a') && (time[i+1] == 'M' || time[i+1] =='m')) {
            if (hh > 11) hh = 0;
            break;
        }
        if (time[i] == '\0') {
            break;
        }
        count++;
    }

    time[0] = hh/10 + '0';
    time[1] = hh%10 + '0';

    char* newTime = (char*) malloc(count* sizeof (char));
    strncpy(newTime, time, count);
        printf("%s", newTime);
    return 0;
}

Saturday, May 7, 2016

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void exchange(int *a, int* b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int partition (int* arr, int si, int ei) {
    int x = arr[ei];
    int i = si-1;
    int j;
    for (j = si; j < ei; j++) {
        if (arr[j] <= x) {
            i++;
            exchange(&arr[j], &arr[i]);
        }
    }
    exchange (&arr[i+1], &arr[ei]);
    return i+1;
}
void quickSort (int* arr, int si, int ei) {
    int pi;
    if (si < ei) {
        pi = partition(arr, si, ei);
        quickSort(arr, si, pi -1);
        quickSort(arr, pi+1, ei);
    }
}
int* twoSum(int* nums, int numsSize, int target) {
      int start = 0, end = numsSize-1, sum,a, b;
      int *temp = malloc(numsSize*sizeof(int));
      int k;
      for (k=0; k <= end; k++) {
          temp[k] = nums[k];
      }
     
      int* index = malloc(2*sizeof(int));
     
      quickSort(temp, start, end);
      int l;
      while (start < end) {
          sum = temp[start] + temp[end];
          if (sum == target) {
              a = temp[start];
              b = temp[end];
              break;
          } else if (sum > target)
            end--;
          else
            start++;
      }
      int z, y;
     
      for(z = 0; z < numsSize; z++) {
          if (nums[z] == a) {
             
              break;
          }
      }
      for (y = 0; y < numsSize; y++) {
          if(nums[y] == b && y!=z) {
             
              break;
          }
      }
      if (z < y) {
          index[0] = z;
          index[1] = y;
      }else {
          index[0] = y;
          index[1] = z;
      }

     
      return index;
}