8000 Merge pull request #2 from VAR-solutions/dev · glitches-coder/Algorithms@b2298e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2298e1

Browse files
authored
Merge pull request VAR-solutions#2 from VAR-solutions/dev
updates
2 parents db9c338 + 2602759 commit b2298e1

File tree

55 files changed

+2758
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2758
-35
lines changed

.github/workflows/c-cpp.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: C/C++ CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: configure
17+
run: ./configure
18+
- name: make
19+
run: make
20+
- name: make check
21+
run: make check
22+
- name: make distcheck
23+
run: make distcheck

Arduino_Code/Blink without delay

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Blink without Delay
3+
4+
Turns on and off a light emitting diode (LED) connected to a digital pin,
5+
without using the delay() function. This means that other code can run at the
6+
same time without being interrupted by the LED code.
7+
8+
The circuit:
9+
- Use the onboard LED.
10+
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
11+
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
12+
is set to the correct LED pin independent of which board is used.
13+
If you want to know what pin the on-board LED is connected to on your
14+
Arduino model, check the Technical Specs of your board at:
15+
https://www.arduino.cc/en/Main/Products
16+
17+
created 2005
18+
by David A. Mellis
19+
modified 8 Feb 2010
20+
by Paul Stoffregen
21+
modified 11 Nov 2013
22+
by Scott Fitzgerald
23+
modified 9 Jan 2017
24+
by Arturo Guadalupi
25+
26+
This example code is in the public domain.
27+
28+
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
29+
*/
30+
31+
// constants won't change. Used here to set a pin number:
32+
const int ledPin = LED_BUILTIN;// the number of the LED pin
33+
34+
// Variables will change:
35+
int ledState = LOW; // ledState used to set the LED
36+
37+
// Generally, you should use "unsigned long" for variables that hold time
38+
// The value will quickly become too large for an int to store
39+
unsigned long previousMillis = 0; // will store last time LED was updated
40+
41+
// constants won't change:
42+
const long interval = 1000; // interval at which to blink (milliseconds)
43+
44+
void setup() {
45+
// set the digital pin as output:
46+
pinMode(ledPin, OUTPUT);
47+
}
48+
49+
void loop() {
50+
// here is where you'd put code that needs to be running all the time.
51+
52+
// check to see if it's time to blink the LED; that is, if the difference
53+
// between the current time and last time you blinked the LED is bigger than
54+
// the interval at which you want to blink the LED.
55+
unsigned long currentMillis = millis();
56+
57+
if (currentMillis - previousMillis >= interval) {
58+
// save the last time you blinked the LED
59+
previousMillis = currentMillis;
60+
61+
// if the LED is off turn it on and vice-versa:
62+
if (ledState == LOW) {
63+
ledState = HIGH;
64+
} else {
65+
ledState = LOW;
66+
}
67+
68+
// set the LED with the ledState of the variable:
69+
digitalWrite(ledPin, ledState);
70+
}
71+
}

Arduino_Code/Knock Sensor

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Knock Sensor
3+
4+
This sketch reads a piezo element to detect a knocking sound.
5+
It reads an analog pin and compares the result to a set threshold.
6+
If the result is greater than the threshold, it writes "knock" to the serial
7+
port, and toggles the LED on pin 13.
8+
9+
The circuit:
10+
- positive connection of the piezo attached to analog in 0
11+
- negative connection of the piezo attached to ground
12+
- 1 megohm resistor attached from analog in 0 to ground
13+
14+
created 25 Mar 2007
15+
by David Cuartielles <http://www.0j0.org>
16+
modified 30 Aug 2011
17+
by Tom Igoe
18+
19+
This example code is in the public domain.
20+
21+
http://www.arduino.cc/en/Tutorial/Knock
22+
*/
23+
24+
25+
// these constants won't change:
26+
const int ledPin = 13; // LED connected to digital pin 13
27+
const int knockSensor = A0; // the piezo is connected to analog pin 0
28+
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
29+
30+
31+
// these variables will change:
32+
int sensorReading = 0; // variable to store the value read from the sensor pin
33+
int ledState = LOW; // variable used to store the last LED status, to toggle the light
34+
35+
void setup() {
36+
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
37+
Serial.begin(9600); // use the serial port
38+
}
39+
40+
void loop() {
41+
// read the sensor and store it in the variable sensorReading:
42+
sensorReading = analogRead(knockSensor);
43+
44+
// if the sensor reading is greater than the threshold:
45+
if (sensorReading >= threshold) {
46+
// toggle the status of the ledPin:
47+
ledState = !ledState;
48+
// update the LED pin itself:
49+
digitalWrite(ledPin, ledState);
50+
// send the string "Knock!" back to the computer, followed by newline
51+
Serial.println("Knock!");
52+
}
53+
delay(100); // delay to avoid overloading the serial port buffer
54+
}

Backtracking/Nqueens.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
5+
public class Nqueens
6+
{
7+
static HashSet<Integer> ldia = new HashSet<Integer>();
8+
static HashSet<Integer> rdia = new HashSet<Integer>();
9+
public static void find(int count,int ro,HashSet<Integer> row,HashSet<Integer> col,int A,ArrayList<String> temp,StringBuilder build,StringBuilder dummy,ArrayList<ArrayList<String>> ans)
10+
{
11+
if(count == A)
12+
{
13+
ans.add(new ArrayList<String>(temp));
14+
return;
15+
}
16+
for(int i=0;i<A;i++)
17+
{
18+
if(!row.contains(ro) && !col.contains(i) && !ldia.contains(ro + i) && !rdia.contains(A - i - 1 + ro))
19+
{
20+
build = new StringBuilder(dummy);
21+
build.setCharAt(i,'Q');
22+
temp.add(new String(build.toString()));
23+
row.add(ro);col.add(i);ldia.add(ro + i);rdia.add(A - i - 1 + ro);
24+
find(count+1,ro+1,row,col,A,temp,build,dummy,ans);
25+
row.remove(ro);col.remove(i);ldia.remove(ro+i);rdia.remove(A - i - 1 + ro);
26+
build = new StringBuilder(dummy);
27+
temp.remove(temp.size() - 1);
28+
}
29+
}
30+
}
31+
public static ArrayList<ArrayList<String>> solveNQueens(int A)
32+
{
33+
StringBuilder build = new StringBuilder();
34+
StringBuilder dummy = new StringBuilder();
35+
ArrayList<ArrayList<String>> ans = new ArrayList<>();
36+
for(int i=0;i<A;i++)
37+
{
38+
build.append(".");
39+
dummy.append(".");
40+
}
41+
HashSet<Integer> row = new HashSet<>();
42+
HashSet<Integer> col = new HashSet<>();
43+
ArrayList<String> temp = new ArrayList<>();
44+
find(0,0,row,col,A,temp,build,dummy,ans);
45+
return ans;
46+
}
47+
public static void main(String[] args)
48+
{
49+
Scanner in = new Scanner(System.in);
50+
System.out.print("Enter the size of ChessBoard : ");
51+
int n = in.nextInt();
52+
ArrayList<ArrayList<String>> ans = solveNQueens(n);
53+
for(int i=0;i<ans.size();i++)
54+
{
55+
System.out.println("Solution : " + (i+1));
56+
ArrayList<String> temp = ans.get(i);
57+
for(int j=0;j<temp.size();j++)
58+
{
59+
System.out.println(temp.get(j));
60+
}
61+
}
62+
}
63+
}

Backtracking/Permutation/permuta.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
3+
void troca(int vetor[], int i, int j)
4+
{
5+
int aux = vetor[i];
6+
vetor[i] = vetor[j];
7+
vetor[j] = aux;
8+
}
9+
10+
void permuta(int vetor[], int inf, int sup)
11+
{
12+
10000 if(inf == sup)
13+
{
14+
for(int i = 0; i <= sup; i++)
15+
printf("%d ", vetor[i]);
16+
printf("\n");
17+
}
18+
else
19+
{
20+
for(int i = inf; i <= sup; i++)
21+
{
22+
troca(vetor, inf, i);
23+
permuta(vetor, inf + 1, sup);
24+
troca(vetor, inf, i);
25+
}
26+
}
27+
}
28+
29+
int main(int argc, char *argv[])
30+
{
31+
int v[] = {1, 2, 3, 4};
32+
int tam_v = sizeof(v) / sizeof(int);
33+
34+
permuta(v, 0, tam_v - 1);
35+
36+
return 0;
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//Counting the number of flipped bits to be flipped to covert A into B
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
6+
/* Name of the class has to be "Main" only if the class is public. */
7+
public class FlipBits
8+
{
9+
public static long countFlippedBits(long A,long B){
10+
long ans=A^B;
11+
long count=0;
12+
while(ans>0){
13+
count+=ans&1;
14+
ans>>=1;
15+
}
16+
return count;
17+
}
18+
public static void main (String[] args) throws java.lang.Exception
19+
{
20+
// your code goes here
21+
Scanner scan=new Scanner(System.in);
22+
long A=scan.nextLong();
23+
long B=scan.nextLong();
24+
long ans=countFlippedBits(A,B);
25+
System.out.println(ans);
26+
}
27+
}

Bit Manipulation/swap/Java/Swap.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
class Swap {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
// Input 2 integers
10+
int a = Integer.parseInt(br.readLine());
11+
int b = Integer.parseInt(br.readLine());
12+
a = a^b;
13+
b = a^b;
14+
a = a^b;
15+
16+
// Integers swapped
17+
System.out.println(a +" "+ b);
18+
}
19+
}

Cryptography/CeaserCipher/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ceaser's Cipher is one of the oldest classical ciphers and also one of the simplest.
2+
In this classical symmetric cipher, The characters are shifted by a fixed value called the key, to encrypt.
3+
This Algorithm uses frequency analysis to break the ceaser's cipher on a ciphertext.

Divide and Conquer /closest_points.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import math
2+
3+
# This program takes n points in a plane as input and returns the closest pair of points. The algorithm is based on divide
4+
# and conquer paradigm. In this program, it is assumed that no two points have same x coordinates of same y coordinates (If not
5+
# we can enforce this by slight rotation of plane)
6+
7+
8+
# Function to find distance between two points
9+
def dist(P1,P2):
10+
x1,y1=P1
11+
x2,y2=P2
12+
return math.sqrt((x2-x1)**2 + (y2-y1)**2)
13+
14+
15+
# This is trivial method to find distance between two points. We definitely will not divide plane when there will be only two or three
16+
# vertices in a plane. So, in such cases distance will be found using brute force method
17+
def brute_force(a):
18+
m=10**10
19+
for i in range(len(a)):
20+
for j in range(i+1,len(a)):
21+
m=min(dist(a[i],a[j]),m)
22+
return m
23+
24+
25+
# This function finds the closest pair of points such that both the points lies in different parts of plane
26+
# that is divided in two parts
27+
def stripClosest(strip,d):
28+
m=d
29+
strip.sort(key=lambda t:t[1])
30+
31+
for i in range(len(strip)):
32+
for j in range(i+1,len(strip)):
33+
if strip[j][1]-strip[i][1]<m:
34+
m= min(m,dist(strip[i],strip[j]))
35+
return m
36+
37+
38+
# This function divides whole plane in two parts P1 and P2 and finds shortest distance pair in both the planes
39+
# recursively, then it checks if the pair of shortest distance is such that one point lies in P1 and another lies
40+
# in P2. If this is the case, then it updates the minimum distance pair, otherwise it simply outputs the shortest
41+
# distance pair found in any one part.
42+
def closest(a):
43+
if len(a)<=3:
44+
return brute_force(a)
45+
46+
m=len(a)/2
47+
dl=closest(a[:m+1])
48+
dr=closest(a[m+1:])
49+
mp=a[m]
50+
d=min(dl,dr)
51+
52+
strip=[]
53+
54+
for i in a:
55+
x,y=i
56+
if abs(x-mp[0])<d:
57+
strip.append(i)
58+
return min(d,stripClosest(strip,d))
59+
60+
61+
print("Enter number of vertices in a plane")
62+
n=input()
63+
64+
#Empty array of size n
65+
a=[None]*n
66+
67+
# Taking vertices in input, (x,y) are the coordinates of points
68+
print("Enter vertices in the form of (x,y) coordinates")
69+
for _ in range(n):
70+
x,y=map(int,raw_input().split())
71+
a[_]=(x,y)
72+
73+
74+
#sort the input array according to x coordinate
75+
a.sort()
76+
print closest(a)
77+
78+
79+
80+
#----------Test Case------------#
81+
82+
'''
83+
{{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}}
84+
This input gives 1.414214 output
85+
'''
86+

0 commit comments

Comments
 (0)
0