Skip to content

Instantly share code, notes, and snippets.

@gauravrock
Created June 26, 2018 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravrock/2e2002cf9019203dfc2ab9a6d37918f3 to your computer and use it in GitHub Desktop.
Save gauravrock/2e2002cf9019203dfc2ab9a6d37918f3 to your computer and use it in GitHub Desktop.
Code-Base

Code-Base

A code-base for a series of problems for programming currently in C and Java (only front-end services included).

A Pen by Gaurav sharma on CodePen.

License.

<header>
<nav class="navbar ">
<a class="navbar-brand py-0" href="#">
<i class="brand material-icons pt-3">code</i>
</a>
<i id="admin" class="material-icons btn my-sm-0 mr-5">
account_circle
</i>
</nav>
<div id="mySidenav" class="sidenav">
<h1 class="text-white"> &lt;Problems&gt;</h1>
<hr class="bg-light">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'palin')">Palindrome problem</a>
<a class="tablinks" onclick="openCity(event, 'fibon')">Fibonacci series problem</a>
<a class="tablinks" onclick="openCity(event, 'facto')">Factorial Number problem</a>
<a class="tablinks" onclick="openCity(event, 'bubbl')">Bubble sort problem</a>
<a class="tablinks" onclick="openCity(event, 'find3')">Find 3rd largest number in an array</a>
</div>
</header>
<!--header ends here-->
<!--main starts here-->
<main>
<div class="alert alert-warning" role="alert">
<b>oops!</b> we're not connected to backend services at present.
</div>
<div>
<div class="jumbotron ">
<div class="welcome">
<h1 style="text-align: center;">Welcome to &lt;code-base&gt; </h1>
<h3 style="text-align: center;">Tap
<i class="start_button material-icons" style="font-size:50px; border-radius: 50%; color:antiquewhite; background-color: black; margin-top: 30px;">
chevron_right</i>
on left side of screen to choose the code-problems.</h3>
</div>
<section id="palin" class="tabcontent">
<h2>Palindrome problem</h2>
<p>A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461, for example, it is "symmetrical". The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed.
</p>
<div class="terminal">
<button type="button" class="btn btn-light C">C</button>
<button type="button" class="btn btn-warning Java">Java</button>
<pre class="one"><code>
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
// A function to check if a string str is palindrome
void isPalindrome(char str[])
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = strlen(str) - 1;
// Keep comparing characters while they are same
while (h > l)
{
if (str[l++] != str[h--])
{
printf("%s is Not Palindrome", str);
return;
}
}
printf("%s is palindrome", str);
}
// Driver program to test above function
int main()
{
isPalindrome("abba");
isPalindrome("abbccbba");
isPalindrome("geeks");
return 0;
}
</code></pre>
<pre class="two"><code>
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
</code></pre>
</div>
</section>
<section id="fibon" class="tabcontent">
<h2>Fibonacci series problem</h2>
<p> In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.</p>
<div class="terminal">
<button type="button" class="btn btn-light C">C</button>
<button type="button" class="btn btn-warning Java">Java</button>
<pre class="one">
<code>
#include &lt;stdio.h&gt;
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&amp;number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i&lt;number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
</code>
</pre>
<pre class="two"><code>
class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i&amp;count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
</code></pre>
</div>
</section>
<section id="facto" class="tabcontent">
<h2>Factorial Number problem</h2>
<p>Factorial is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also, n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.</p>
<div class="terminal">
<button type="button" class="btn btn-light C">C</button>
<button type="button" class="btn btn-warning Java">Java</button>
<pre class="one"><code>
#include &lt;stdio.h&gt;
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &amp;n);
for (c = 1; c &lt;= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
</code></pre>
<pre class="two"><code>
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n &gt; 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c &lt;= n; c++)
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
</code></pre>
</div>
</section>
<section id="bubbl" class="tabcontent">
<h2>Bubble sort problem</h2>
<p>Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.</br>
Example:
First Pass:</br>
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.</br>
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4</br>
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2</br>
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.</br>
Second Pass:</br>
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )</br>
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br>
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.</br>
Third Pass:</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br>
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</p>
<div class="terminal">
<button type="button" class="btn btn-light C">C</button>
<button type="button" class="btn btn-warning Java">Java</button>
<pre class="one"><code>
/* Bubble sort code */
#include &lt;stdio.h&gt;
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &amp;n);
printf("Enter %d integers\n", n);
for (c = 0; c &lt; n; c++)
scanf("%d", &amp;array[c]);
for (c = 0 ; c &lt; n - 1; c++)
{
for (d = 0 ; d &lt; n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use &lt; */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c &lt; n; c++)
printf("%d\n", array[c]);
return 0;
}
</code></pre>
<pre class="two"><code>
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i &lt; n; i++){
for(int j=1; j &lt; (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println("Array Before Bubble Sort");
for(int i=0; i &lt; arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);//sorting array elements using bubble sort
System.out.println("Array After Bubble Sort");
for(int i=0; i &lt; arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
</code></pre>
</div>
</section>
<section id="find3" class="tabcontent">
<h2>Find 3rd largest number in an array</h2>
<p>We can find the third largest number in an array in java by sorting the array and returning the 3nd largest number. Let's see the full example to find the third largest number in java array.</p>
<div class="terminal">
<button type="button" class="btn btn-light C">C</button>
<button type="button" class="btn btn-warning Java">Java</button>
<pre class="one"><code>
/*program to print 3 rd greatest number*/
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
main( )
{
int a[20],i,j,n,t,b;
printf("the value of n:");
scanf("%d\n",&amp;n);
printf("enter all array elements to be sort\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i&lt;n;i++)
{
printf("\t%d\",a[i]);
}
b=a[n-3];
printf("the third greatest number is\n");
printf("%d",b);
getch( );
}
</code></pre>
<pre class="two"><code>
public class ThirdLargestInArrayExample{
public static int getThirdLargest(int[] a, int total){
int temp;
for (int i = 0; i &lt; total; i++)
{
for (int j = i + 1; j &lt; total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-3];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Third Largest: "+getThirdLargest(a,6));
System.out.println("Third Largest: "+getThirdLargest(b,7));
}}
</code></pre>
</div>
</section>
</div>
</div>
<i class="animated infinite rubberBand side-button material-icons" style="font-size:30px;cursor:pointer" onclick="openNav()">
chevron_right</i>
</main>
console.log("hello");
$('.alert').hide()
$('#admin').click(function() {
$(".alert")
.slideDown(1000)
.delay(2000)
.slideUp();
});
$('.two').hide();
$('.Java').click(function() {
$(".one").hide();
$(".two").show();
});
$('.C').click(function() {
$(".two").hide();
$(".one").show();
});
//to hide the start_page when a link is clicked
$('.tablinks').click(function() {
$(".welcome").hide();
});
//adding functionality to sidenav
function openNav() {
document.getElementById("mySidenav").style.width = "100%";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
//to select a particular problem from sidenav
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
document.getElementById("mySidenav").style.width = "0";
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
@import url(https://fonts.googleapis.com/css?family=Luckiest+Guy);
@import url(https://fonts.googleapis.com/css?family=Acme);
@import
url(https://fonts.googleapis.com/icon?family=Material+Icons);
body{
overflow: hidden;
background: linear-gradient(90deg, #21E3E3, #0453E8);
}
.navbar{
background: linear-gradient(90deg, #173F57, #40769F);
box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.2), 0 8px 20px 0 rgba(0, 0, 0, 0.19);
}
#admin{
font-size: 50px;
color:#49C2D8;
}
#admin:hover{
color:#81D7E6;
text-shadow: 1px 8px 40px #020202;
}
h1{
font-family: 'Luckiest Guy';
}
/*to change the size and style of side button icon*/
.side-button{
margin-top: 280px;
font-size: 4em !important;
margin-left: -35px;
color:white;
opacity: 0.9;
border-radius: 55px;
background-color: #020202;
}
/*to change the style for sidenav*/
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #182934;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
text-align:center;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181 !important;
display: block;
transition: 0.3s;
font-family: 'Acme', sans-serif;
}
.sidenav a:hover{
color: #f1f1f1 !important;
cursor:pointer !important;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
.jumbotron{
width:90%;
height:80%;
margin: 25px 5%;
position:absolute;
box-shadow: 0 8px 8px 0 rgba(0, 0, 0, 0.2), 0 8px 20px 0 rgba(0, 0, 0, 0.19);
overflow: scroll;
}
.tabcontent{
display:none;
}
h2{
text-align: center;
font-family: 'Acme', sans-serif;
}
.terminal{
background-color:#172D39;
border-radius: 2%;
overflow-y:scroll !important;
}
code{
color:white !important;
}
pre{
margin-left: 50px !important;
text-align: left !important;
white-space: pre-line;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
@media only screen and (max-width: 600px) {
pre{
margin-left: 5px !important;
text-align: left;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment