Sunday, 4 December 2022

Learning more will never be a disadvantage

Hello world, 🙏

Every novice in programming get stuck with a basic problem WHICH LANGUAGE DO THEY PREFER? They are advised by the most to not follow C language. They should directly jump into some language supporting OOPs. Well, I disagree here as C language gives you an encapsulated feel of MEMORY architecture which is paramount topic in CS . Also, learning more will never be a disadvantage. It will only act as a boon.

You can read an article by Mario Galindo Queralt related to this topic below which I found on Quora. I am attaching the link of the same below.

Question:  

Why do universities teach C and C++, though they are regarded as the most difficult languages and there are better alternatives? C and C++ aren’t expanding and will be dead in 30 years, but Stanford, Harvard and U Mich use C++ as a primary language.

 

Answer:

You are totally uninformed. C++ is expanding a lot. The latest version is C++17, next year the C++20 version is being issued, the committee is working hard on C++23 and C++26 is scheduled.

You believe that C and C++ will be dead in 30 years. That belief is based entirely on unsubstantiated speculation. If you don't know anything about the present, less can you know about the future.

In spite of that, 30 years of prognosis in the future is a great deal of time for CS. Therefore, it is feasible to consider that everything, yes EVERYTHING, what exists today in CS will somehow be dead in 30 years. Based on this presumption, should you, and everyone, stop studying absolutely everything today?

Stanford and Harvard are among the most important engineering universities in the world. You must trust their teachers when they choose C and C++ as their primary computer languages. Believe me, they know much more than you.

Therefore, if something is difficult for you, I recommend that you study it more, instead of looking for false arguments to modify what is taught in the university. Do not pretend to teach your teachers, it is the other way around, they are the ones who teach and you who learn.

Regards.

Link to read above on Quora:  Why do universities teach C and C++, though they are regarded as the most difficult languages and there are better alternatives? C and C++ aren’t expanding and will be dead in 30 years, but Stanford, Harvard and U Mich use C++ as a primary language. - Quora

Friday, 30 September 2022

Problem Statement : 

Program to Implement two Stacks using a Single Array & Check for                                      Overflow & Underflow

My code:


//Here is my solution to this question in C language

//I have taken the size of array containing STACK 1 and STACK 2 as 10. Readers may change it as per their need


#include <stdio.h>

#include <stdlib.h>

#define size 10

int arr[size];

int top1=-1;

int top2=size;

//push() for STACK 1:

void push_stack_1(int value)

{

    if(top1<top2-1)

    {

        top1=top1+1;

        arr[top1]=value;

    }

    else

    {

        printf("STACK 1 OVERFLOW\n");

    }

}

//push() for STACK 2:

void push_stack_2(int value)

{

    if(top1<top2-1)

    {

        top2=top2-1;

        arr[top2]=value;

    }

    else

    {

        printf("STACK 2 OVERFLOW\n");

    }

}


//pop() for STACK 1:

void pop_stack_1()

{

    if(top1>=0)

    {

        int popped_value=arr[top1];

        top1=top1-1; 

        printf("%d is popped from STACK 1\n",popped_value);

    }

    else

    {

        printf("STACK 1 UNDERFLOW\n");

    }

}


//pop() for STACK 2:

void pop_stack_2()

{

    if(top2<size)

    {

        int popped_value=arr[top2];

        top2=top2+1; 

        printf("%d is popped from STACK 2\n",popped_value);

    }

    else

    {

        printf("STACK 2 UNDERFLOW\n");

    }

}


//Displaying


//display() for STACK 1:

void display_stack_1()

{

    int i;

    printf("The values of STACK 1 in LIFO manner is:\n");

    for(i=top1;i>=0;i--)

    {

        printf("%d\n",arr[i]);

    }

}


//display() for STACK 2:

void display_stack_2()

{

    int i;

     printf("The values of STACK 2 in LIFO manner is:\n");

    for(int i=top2;i<size;i++)

    {

        printf("%d\n",arr[i]);

    }

}



 int main()  

{  

  int arr[size];  

  int i;  

  int num_of_ele;  

   

  printf ("We can push a total of 10 values\n"); 

  printf("-----------------------------------------------------------");

printf("\n"); 

   

  //Number of elements pushed in stack 1 is 5 

  //Number of elements pushed in stack 2 is 5 

   

// loop to insert the elements into Stack1    

for (i = 1; i <= 5; i++)  

  {  

    push_stack_1(i);  

    printf ("Value Pushed in Stack 1 is %d\n", i);  

  }  

 printf("-----------------------------------------------------------");

printf("\n");  

// loop to insert the elements into Stack2.    

for (i = 6; i <= 10; i++)  

  {  

    push_stack_2(i);  

    printf ("Value Pushed in Stack 2 is %d\n", i);  

  }  

   

  //Print Both Stacks  

 display_stack_1(); 

 printf("-----------------------------------------------------------");

printf("\n"); 

 display_stack_2 ();  

   

  //Pushing on Stack Full  

  printf ("Pushing Value in Stack 1 is %d\n", 11);  

  push_stack_1 (11); 

  printf ("Pushing Value in Stack 2 is %d\n", 110);  

  push_stack_2 (110);   

printf("-----------------------------------------------------------");

printf("\n");

  //Popping All Elements from Stack 1  

  num_of_ele = top1 + 1;  

  while (num_of_ele)  

  {  

    pop_stack_1 ();  

    num_of_ele--;  

  }  

printf("-----------------------------------------------------------");

printf("\n");

num_of_ele = top2 + 1;  

  while (num_of_ele)  

  {  

    pop_stack_2 ();  

    num_of_ele--;  

  }  

    

  // Trying to Pop the element From the Empty Stack  

printf("-----------------------------------------------------------");

printf("\n");

printf("Trying to Pop the element From the Empty Stack 1\n");

pop_stack_1 ();   

printf("-----------------------------------------------------------");

printf("\n");

printf("Trying to Pop the element From the Empty Stack 2\n");

pop_stack_2 ();   

return 0;  

Tuesday, 9 August 2022

 

'''
problem--
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Function Description--
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.
timeConversion has the following parameter(s):
s: a string representing time in 12 hour format
Input Format--
A single string containing a time in 12-hour clock format (i.e.:hh:mm:ssAM or hh:mm:ssPM) ,where 00<=hh<=12 and 00<=mm,ss<=59.
Constraints--
All input times are valid
Output Format--
Convert and print the given time in 24-hour format, where 00<=hh<=23.
Sample Input 0--
07:05:45PM
Sample Output 0--
19:05:45
'''
# Here is my solution to the question given in HackerRank site
s=input()
time=""
if (s[8:10]=='AM' and int(s[0:2]+s[3:5]+s[6:8])<=116059):
    time=s[0:8]
    print(time)

elif (s[8:10]=='AM' and int(s[0:2]+s[3:5]+s[6:8])>=120000 and int(s[0:2]+s[3:5]+s[6:8])<=126059):
    #time=str(int(s[0:2])-12)+'0'+s[2:8]  
    time=str(int(s[0:2])-12)+'0'+s[2:8]
    print(time)

elif (s[8:10]=='PM' and int(s[0:2]+s[3:5]+s[6:8])>=120000 and int(s[0:2]+s[3:5]+s[6:8])<=126059):
    time=s[0:8]
    print(time)


else:
    time=str((int(s[0:2]))+12)+s[2:8]
    print(time)
    






Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...