diff --git a/Programming Challenges - Tech extra work/Candies/Candies.cs b/Programming Challenges - Tech extra work/Candies/Candies.cs new file mode 100644 index 0000000..68ab385 --- /dev/null +++ b/Programming Challenges - Tech extra work/Candies/Candies.cs @@ -0,0 +1,146 @@ + +/* Problem +Alice is a kindergarden teacher. She wants to give some candies to the children +in her class. All the children sit in a line and each of them has a rating +score according to his or her usual performance. Alice wants to give at least +1 candy for each child.Children get jealous of their immediate neighbors, so if +two children sit next to each other then the one with the higher rating must get +more candies. Alice wants to save money, so she wants to minimize the total +number of candies. + +Input + +The first line of the input is an integer N, the number of children in Alice’s +class. Each of the following N lines contains an integer indicates the rating +of each child. + +Ouput + +Output a single line containing the minimum number of candies Alice must give. + +*/ + +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + // A class which stores a students score and the amount of candies that they have + public class Student + { + public int score = 0; + public int candies = 0; + } + + static void Main(String[] args) { + /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ + + int numberOfStudents = int.Parse( Console.ReadLine() ); + + // Create a list to store all students and a variable to store last student procesed + List listOfStudents = new List(); + var lastStudent = new Student(); + + // Store the amount of candies to award the student + int candyAwarded = 1; + + for ( int i = 0; i < numberOfStudents; i++ ) + { + Student newStudent = CreateStudent( int.Parse( Console.ReadLine() )); + + + if ( newStudent.score > lastStudent.score ) + { + newStudent.candies = candyAwarded; + candyAwarded++; + listOfStudents.Add( newStudent ); + lastStudent = newStudent; + } + else if ( newStudent.score < lastStudent.score ) + { + candyAwarded = 1; + newStudent.candies = candyAwarded; + UpdateLeft( ref listOfStudents , newStudent); + listOfStudents.Add( newStudent); + candyAwarded++; + lastStudent = newStudent; + } + else + { + + candyAwarded = 1; + newStudent.candies = candyAwarded; + listOfStudents.Add( newStudent ); + lastStudent = newStudent; + candyAwarded++; + } + } + int count = 0; + + foreach ( var stud in listOfStudents) + { + count += stud.candies; + } + Console.WriteLine( count); + + } + + public static Student CreateStudent( int score ) + { + Student tmpStud = new Student(); + tmpStud.score = score; + return tmpStud; + } + + public static void UpdateLeft( ref List listOfStudents , Student next ) + { + if ( listOfStudents.Count <= 1) + { + if ( listOfStudents[0].score > next.score) + listOfStudents[0].candies++; + + return; + } + int endOfList = listOfStudents.Count -1; + + for ( int i = endOfList; i >= 1; i--) + { + + if ( listOfStudents[i - 1].score > listOfStudents[i].score) + { + listOfStudents[i].candies++; + } + + + if ( listOfStudents[i -1 ].score == listOfStudents[i].score ) + { + listOfStudents[i].candies++; + return; + } + + if ( listOfStudents[i -1 ].score <= listOfStudents[i].score) + { + if ( i == endOfList) + { + return; + } + if ( listOfStudents[i].candies <= listOfStudents[i + 1].candies ) + { + listOfStudents[i].candies = listOfStudents[i + 1].candies + 1; + } + return; + } + } + + + if ( listOfStudents[0].score > listOfStudents[1].score) + { + listOfStudents[0].candies = listOfStudents[1].candies + 1; + } + + return; + + + } + +} \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/Compare the Triplets/compare_the_triplets.py b/Programming Challenges - Tech extra work/Compare the Triplets/compare_the_triplets.py new file mode 100644 index 0000000..223a2d3 --- /dev/null +++ b/Programming Challenges - Tech extra work/Compare the Triplets/compare_the_triplets.py @@ -0,0 +1,32 @@ +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# Complete the compareTriplets function below. +def compareTriplets(a, b): + alice=0 + bob=0 + for i in range(3): + if(a[i]>b[i]): + alice+=1 + elif(a[i] index : + line = line + row[index] + line = line + " " + + print line + +def main(): + #testString = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots" + testString = "feedthedog" + testString = testString.strip() + + stringLen = len(testString) + print stringLen + root = math.sqrt( stringLen ) + topRoot = int(math.ceil( root ) ) + + matrix = splitIntoEncrpytMatrix( testString , topRoot ) + + + + printResult( matrix , topRoot ) + + + +main() + diff --git a/Programming Challenges - Tech extra work/Flowers/Flowers.cs b/Programming Challenges - Tech extra work/Flowers/Flowers.cs new file mode 100644 index 0000000..dab0b79 --- /dev/null +++ b/Programming Challenges - Tech extra work/Flowers/Flowers.cs @@ -0,0 +1,70 @@ + +/* Problem +You and your K-1 friends want to buy N flowers. Flower number i has cost ci. +Unfortunately the seller does not like a customer to buy a lot of flowers, +so he tries to change the price of flowers for customer who had bought flowers +before. More precisely if a customer has already bought x flowers, he should pay + (x+1)*ci dollars to buy flower number i. + +You and your K-1 friends want to buy all N flowers in such a way that you spend +the least amount of money possible. You can buy the flowers in any order. + +Input: + +The first line of input contains two integers N and K (K <= N) next line +contains N positive integers c1,c2,…,cN respectively. + +Output: + +Print the minimum amount of money you (and your friends) have to pay in order to +buy all N flowers. + +*/ + +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + static void Main(String[] args) { + + int N, K; + string NK = Console.ReadLine(); + string[] NandK = NK.Split(new Char[] {' ', '\t', '\n'}); + N = Convert.ToInt32(NandK[0]); + K = Convert.ToInt32(NandK[1]); + + int [] C = new int [N]; + + string numbers = Console.ReadLine(); + string[] split = numbers.Split(new Char[] {' ', '\t', '\n'}); + + int i = 0; + + foreach (string s in split) + { + if( s.Trim() != "") + { + C[i++] = Convert.ToInt32(s); + } + } + + Array.Sort( C ); + + int total = 0; + int bought = 0; + for ( int i = N - 1; i >= 0; i -= K ) + { + for ( int j = 0; j < K; j++ ) + { + if ( i - j >= 0) + { + total += ( bought + 1 ) * C[i - j]; + } + } + bought++; + } + + Console.WriteLine(total); + } +} \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/Mark and toys/MarkAndToys.cs b/Programming Challenges - Tech extra work/Mark and toys/MarkAndToys.cs new file mode 100644 index 0000000..341c770 --- /dev/null +++ b/Programming Challenges - Tech extra work/Mark and toys/MarkAndToys.cs @@ -0,0 +1,68 @@ + +/* Problem +Mark and Jane are very happy after having their first kid. Their son is very +fond of toys. Therefore, Mark wants to buy some toys for his son. But he has a +limited amount of money. But he wants to buy as many toys as he can. So, he is +in a dilemma and is wondering how he can maximize the number of toys he can buy. +He has N items in front of him, tagged with their prices and he has only K rupees. + + +Now, you being Mark’s best friend have the task to help him buy as many toys for +his son as possible. + +Input Format +The first line will contain two inputs N and K, followed by a line containing N +integers + +Output Format +Maximum number of toys Mark can buy for his son. + +Constraints +1<=N<=105 +1<=K<=109 +1<=price of any toy<=109 +*/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +class Solution { + static void Main(String[] args) + { + + var firstLine = Console.ReadLine().Split(); + + var noItems = int.Parse( firstLine[0] ); + var budget = int.Parse( firstLine[1] ); + + var secondLine = Console.ReadLine().Split(); + + var numbers = new List(); + for( int i = 0; i < noItems; i++ ) + { + int tmp = int.Parse( secondLine[i]); + + if ( tmp < budget ) + numbers.Add( tmp); + } + + numbers.Sort( ( s1 , s2 ) => s1.CompareTo(s2 ) ); + + int cost = 0; + int count = 0; + for( int i =0; i< numbers.Count && cost < budget; i++) + { + cost += numbers[i]; + + if ( cost <= budget) + { + count++; + } + } + + Console.WriteLine( count ); + + } +} \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/MatrixRotation/problem.txt b/Programming Challenges - Tech extra work/MatrixRotation/problem.txt new file mode 100644 index 0000000..a149edf --- /dev/null +++ b/Programming Challenges - Tech extra work/MatrixRotation/problem.txt @@ -0,0 +1,20 @@ +You are given a 2D matrix, a, of dimension MxN and a positive integer R. You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction. + +Rotation of a 4x5 matrix is represented by the following figure. Note that in one rotation, you have to shift elements by one step only (refer sample tests for more clarity). + +matrix-rotation + +It is guaranteed that the minimum of M and N will be even. + +Input Format +First line contains three space separated integers, M, N and R, where M is the number of rows, N is number of columns in matrix, and R is the number of times the matrix has to be rotated. +Then M lines follow, where each line contains N space separated positive integers. These M lines represent the matrix. + +Output Format +Print the rotated matrix. + +Constraints +2 <= M, N <= 300 +1 <= R <= 109 +min(M, N) % 2 == 0 +1 <= aij <= 108, where i ∈ [1..M] & j ∈ [1..N] \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/MatrixRotation/rotation.py b/Programming Challenges - Tech extra work/MatrixRotation/rotation.py new file mode 100644 index 0000000..590b9ad --- /dev/null +++ b/Programming Challenges - Tech extra work/MatrixRotation/rotation.py @@ -0,0 +1,90 @@ +import math + + +def printMatrix( matrix ) : + for row in matrix : + print row + +def CreateTextMatrix( xSize , ySize ) : + matrix = [] + + for x in range( 0 , xSize ) : + matrix.append( [] ) + for y in range( 0 , ySize ) : + matrix[x].append(x + y) + + return matrix + +def ShouldStop( min , max ) : + return min >= max + +def MarkMatrix( matrix , newMatrix , startX , endX , startY , endY , depth , numRot ) : + newMatrix = MarkRow( matrix , newMatrix , 0 , 3 , 0 , 0 , 3 , depth , 1 ) + newMatrix = MarkRow( matrix , newMatrix , 0 , 3 , 3 , 0 , 3 , depth , 1 ) + matrix = MarkColumn( startX , startY , endY , matrix , depth ) + matrix = MarkColumn( endX - 1 , startY , endY , matrix , depth ) + return newMatrix + +def MarkDepths( startX , endX , startY , endY , depth , matrix , newMatrix , numRot ) : + + xSize = endX - startX + ySize = endY - startY + + stop = False + if ( xSize < ySize ) : + stop = ShouldStop( startX , endX ) + else : + stop = ShouldStop( startY , endY ) + + if stop : + return matrix + else : + newMatrix = MarkMatrix( matrix , newMatrix , startX , endX , startY , endY , str(depth) , numRot ) + newMatrix = MarkDepths( startX + 1 , endX - 1 , startY + 1 , endY - 1 , depth + 1 , matrix , newMatrix , numRot) + return newMatrix + +def MarkRow( matrix , newMatrix , topRow , bottomRow , currentRow , startX , endX , val , numRot ) : + for x in range( startX , endX ) : + newX = x + newY = currentRow + for rot in range( 0 , numRot ) : + + if newX == startX and newY != bottomRow : + newY = newY + 1 + elif newX == endX and newY != topRow : + newY = newY - 1 + elif newY == bottomRow : + newX = newX + 1 + else : + newX = newX - 1 + + + newMatrix[newY][newX] = str(matrix[currentRow][x]) + + return newMatrix + + + + +def MarkColumn( xVal , startY , endY , matrix , val ) : + for col in range( startY , endY ) : + matrix[col][xVal] = val + return matrix + + +def MapoutMatrix( matrixToMap , xSize , ySize , newMatrix , rot ) : + matrixToMap = MarkDepths( 0 , xSize , 0 , ySize - 1 , 1, matrixToMap , newMatrix , 1 ) + return matrixToMap + + +xSize = 4 +ySize = 4 +numOfRotations = 1 +testMatrix = CreateTextMatrix( xSize , ySize ) +printMatrix( testMatrix ) +newMatrix = CreateTextMatrix( xSize , ySize ) +newMatrix = MapoutMatrix( testMatrix , xSize , ySize , newMatrix , numOfRotations ) +printMatrix( newMatrix ) + + + diff --git a/Programming Challenges - Tech extra work/MissingNumbers/MissingNumbers.cs b/Programming Challenges - Tech extra work/MissingNumbers/MissingNumbers.cs new file mode 100644 index 0000000..5caebce --- /dev/null +++ b/Programming Challenges - Tech extra work/MissingNumbers/MissingNumbers.cs @@ -0,0 +1,77 @@ + +/* Problem + Given N integers, count the total pairs of integers that have a difference of K. + + Input Format + 1st line contains N & K (integers). + 2nd line contains N numbers of the set. All the N numbers are assured to be distinct. + + Output Format + One integer saying the number of pairs of numbers that have a diff K. + + Constraints: + N <= 10^5 + 0 < K < 10^9 + Each integer will be greater than 0 and at least K away from 2^31-1 +*/ + +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + static void Main(String[] args) { + + int sizeOfA = int.Parse( Console.ReadLine() ); + var a = CreateFrequencyMap( sizeOfA ); + + int sizeOfB = int.Parse( Console.ReadLine() ); + var b = CreateFrequencyMap( sizeOfB ); + + + List tmp = new List(); + string output = ""; + foreach( var val in a ) + { + if ( b.ContainsKey( val.Key )) + { + if ( a[val.Key] < b[val.Key]) + { + tmp.Add( val.Key ); + } + } + } + + tmp.Sort(); + + foreach( var v in tmp ) + { + output += v; + output += " "; + } + + Console.WriteLine( output ); + + } + + public static Dictionary CreateFrequencyMap( int size ) + { + var firstLine = Console.ReadLine().Split(); + + var tmpDict = new Dictionary(); + + for( int i = 0; i < size; i++ ) + { + int tmp = int.Parse( firstLine[i]); + + if ( tmpDict.ContainsKey( tmp ) ) + tmpDict[tmp]++; + else + { + tmpDict.Add( tmp , 1 ); + } + } + return tmpDict; + } + +} \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/NewYearChaos/chaos.py b/Programming Challenges - Tech extra work/NewYearChaos/chaos.py new file mode 100644 index 0000000..dfa6e4a --- /dev/null +++ b/Programming Challenges - Tech extra work/NewYearChaos/chaos.py @@ -0,0 +1,77 @@ + + +gNumSteps = 0 + +def main(): + + sizeOfLine = 5 + firstLine = [2,1,5,3,4] + global gNumSteps + + if checkIfPossible( firstLine ) == False : + return + + currentIndex = isLineCorrect( firstLine, sizeOfLine ) + while ( currentIndex != -1 ) : + firstLine = sortLine( 0 , firstLine , sizeOfLine ) + currentIndex = isLineCorrect( firstLine, sizeOfLine ) + + print gNumSteps + + + +def checkIfPossible( line ) : + + expectedVal = 1 + + for index in range( 0 , len( line) ) : + currentVal = line[index] + diff = currentVal - expectedVal + + if diff > 2 : + print "Cant be done " + return False + else : + expectedVal = expectedVal + 1 + + +def isLineCorrect( line , size ) : + expectedVal = 1 + for index in range( 0, size ) : + if line[index] != expectedVal : + return index + expectedVal = expectedVal + 1 + return -1 + + +def sortLine( startIndex , line , size ) : + + global gNumSteps + + currentIndex = startIndex + expectedVal = startIndex + 1 + while currentIndex < size : + currentVal = line[currentIndex ] + + if currentVal == expectedVal : + currentIndex = currentIndex + 1 + expectedVal = expectedVal + 1 + else : + + if currentIndex < ( size - 1 ) : + gNumSteps = gNumSteps + 1 + line[currentIndex] = line[currentIndex + 1] + line[currentIndex + 1 ] = currentVal + + currentIndex = currentIndex + 1 + expectedVal = expectedVal + 1 + + return line + + +main() + + + + + diff --git a/Programming Challenges - Tech extra work/NewYearChaos/problem.txt b/Programming Challenges - Tech extra work/NewYearChaos/problem.txt new file mode 100644 index 0000000..6aa35e1 --- /dev/null +++ b/Programming Challenges - Tech extra work/NewYearChaos/problem.txt @@ -0,0 +1,20 @@ +It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! + +There are people queued up, and each person wears a sticker indicating their initial position in the queue (i.e.: with the first number denoting the frontmost position). + +Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original place in line. One person can bribe at most two other persons. + +That is to say, if and bribes , the queue will look like this: . + +Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state! + +Note: Each wears sticker , meaning they were initially the person in queue. + +Input Format + +The first line contains an integer, , denoting the number of test cases. +Each test case is comprised of two lines; the first line has (an integer indicating the number of people in the queue), and the second line has space-separated integers describing the final state of the queue. + +Output Format + +Print an integer denoting the minimum number of bribes needed to get the queue into its final state; print Too chaotic if the state is invalid (requires to bribe more than people). \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/Pairs/Pairs.cs b/Programming Challenges - Tech extra work/Pairs/Pairs.cs new file mode 100644 index 0000000..8ed7cda --- /dev/null +++ b/Programming Challenges - Tech extra work/Pairs/Pairs.cs @@ -0,0 +1,52 @@ + +/* Problem + Given N integers, count the total pairs of integers that have a difference of K. + + Input Format + 1st line contains N & K (integers). + 2nd line contains N numbers of the set. All the N numbers are assured to be distinct. + + Output Format + One integer saying the number of pairs of numbers that have a diff K. + + Constraints: + N <= 10^5 + 0 < K < 10^9 + Each integer will be greater than 0 and at least K away from 2^31-1 +*/ + +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + static void Main(String[] args) { + + var line = Console.ReadLine().Split(); + + var numberOfIntegers = int.Parse( line[0] ); + var kDifference = int.Parse( line[1] ); + + + var NumDict = new Dictionary(); + var numbers = Console.ReadLine().Split(); + + for( int i = 0; i < numberOfIntegers; i++ ) + { + var tmp = int.Parse( numbers[i]); + NumDict.Add( tmp , tmp ); + } + + int count = 0; + foreach(KeyValuePair entry in NumDict) + { + if( NumDict.ContainsKey( (int)(entry.Key + kDifference )) ) + count++; + + } + + Console.WriteLine( count ); + + } +} +} \ No newline at end of file diff --git a/Programming Challenges - Tech extra work/RedJohn/RedJohn.cs b/Programming Challenges - Tech extra work/RedJohn/RedJohn.cs new file mode 100644 index 0000000..2c6d50f --- /dev/null +++ b/Programming Challenges - Tech extra work/RedJohn/RedJohn.cs @@ -0,0 +1,143 @@ + +/* Problem +Red John has committed another murder. But this time, he doesn’t leave a red +smiley behind. What he leaves behind is a puzzle for Patrick Jane to solve. + He also texts Teresa Lisbon that if Patrick is successful, he will turn himself + in. The puzzle begins as follows. + +There is a wall of size CxN in the victim’s house where C is the 1st composite +number. The victim also has an infinite supply of bricks of size Cx1 and 1xC in +her house. There is a hidden safe which can only be opened by a particular +configuration of bricks on the wall. In every configuration, the wall has to be +completely covered using the bricks. There is a phone number written on a note in +the safe which is of utmost importance in the murder case. Gale Bertram wants to +know the total number of ways in which the bricks can be arranged on the wall +so that a new configuration arises every time. He calls it M. + +Since Red John is back after a long time, he has also gained a masters degree in +Mathematics from a reputed university. So, he wants Patrick to calculate the +number of prime numbers (say P) up to M (i.e. <= M). If Patrick calculates P, +Teresa should call Red John on the phone number from the safe and he will +surrender if Patrick tells him the correct answer. Otherwise, Teresa will get +another murder call after a week. + +You are required to help Patrick correctly solve the puzzle. + +Sample Input +The first line of input will contain an integer T followed by T lines each +containing an integer N. + +Sample Output +Print exactly one line of output for each test case. The output should contain +the number P. +*/ +using System; +using System.Collections.Generic; +using System.IO; +class Solution { + + static void Main(String[] args) { + + int numTestCases = int.Parse( Console.ReadLine() ); + + numBricks.Add( 1 , 1); + numBricks.Add( 2, 1 ); + numBricks.Add( 3, 1 ); + numBricks.Add( 4 , 2); + + primes.Add( 2); + + for ( int i =0; i < numTestCases; i++ ) + { + int bricks = int.Parse( Console.ReadLine() ); + + int numMoves = GetNumOfPossiblities( bricks); + Console.WriteLine( GetNumOfPrimesBefore( numMoves) ); + + } + } + + static Dictionary numBricks = new Dictionary(); + static int highestBricks = 4; + static public int GetNumOfPossiblities( int bricks ) + { + if ( bricks <= 3) + return 1; + + if ( bricks == 4 ) + return 2; + + if ( bricks <= highestBricks) + { + return numBricks[bricks]; + } + + for ( int i = highestBricks + 1; i <= bricks; i++) + { + int newMoves = numBricks[i - 1] + numBricks[i-4]; + numBricks.Add(i,newMoves); + } + highestBricks = bricks; + return numBricks[bricks]; + } + + static List primes = new List(); + static public int GetNumOfPrimesBefore( int num ) + { + if ( num <= 1) + return 0; + + if ( num <= 2) + return 1; + + if ( num < highestPrimeCheck ) + { + int count = 0; + foreach( int prime in primes ) + { + if ( prime > num ) + { + return count; + } + count++; + } + return count; + } + + GenerateNewPrimes( num ); + + return primes.Count; + + } + + static int highestPrimeCheck = 3; + static void GenerateNewPrimes( int checkUpTo ) + { + for ( int i = highestPrimeCheck; i <= checkUpTo; i+= 2 ) + { + bool isPrime = true; + + foreach( int prime in primes) + { + if ( i % prime == 0) + { + isPrime = false; + break; + } + } + if ( isPrime ) + { + primes.Add( i ); + } + } + + if ( checkUpTo % 2 == 0 ) + { + highestPrimeCheck = checkUpTo + 1; + } + else + { + highestPrimeCheck = checkUpTo; + } + } +} diff --git a/README.md b/README.md deleted file mode 100644 index 9821eee..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# University_Practicals -A place to store my university practicals