Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions Programming Challenges - Tech extra work/Candies/Candies.cs
Original file line number Diff line number Diff line change
@@ -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<Student> listOfStudents = new List<Student>();
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<Student> 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;


}

}
Original file line number Diff line number Diff line change
@@ -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]<b[i]):
bob+=1
return (alice, bob)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

a = list(map(int, input().rstrip().split()))

b = list(map(int, input().rstrip().split()))

result = compareTriplets(a, b)

fptr.write(' '.join(map(str, result)))
fptr.write('\n')

fptr.close()
38 changes: 38 additions & 0 deletions Programming Challenges - Tech extra work/DiagionalSum/DSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys


def countDiag( matrix , startIndex , updateIndex ) :

coloumCount = startIndex
sum = 0
for row in matrix :
sum = sum + row[coloumCount]
coloumCount = updateIndex( coloumCount )

return sum

def PosMove( currentIndex ) :
return currentIndex + 1

def NegMove( currentIndex ) :
return currentIndex - 1

#n = int(raw_input().strip())
#a = []
#for a_i in xrange(n):
# a_temp = map(int,raw_input().strip().split(' '))
# a.append(a_temp)

a = [11,2,4]
b = [4,5,6]
c = [10,8,-12]

n = 3
final = []
final.append( a )
final.append( b)
final.append( c)

posSum = countDiag( final , 0 , PosMove )
negSum = countDiag( final , n - 1, NegMove )
print abs(posSum - negSum)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import math


def splitIntoEncrpytMatrix( testString , rowSize ) :
stringLen = len( testString )

currentIndex = 0
matrix = []
currentRow = []
for letter in testString :
currentRow.append( letter )
currentIndex = currentIndex + 1

if ( currentIndex == rowSize ) :
currentIndex = 0
matrix.append( currentRow )
currentRow = []


if ( len( currentRow) != 0 ) :
matrix.append( currentRow )

return matrix



def printResult( matrix , rowSize ) :
line = ""
for index in range( 0 , rowSize ) :

for row in matrix :
if len( row ) > 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()

70 changes: 70 additions & 0 deletions Programming Challenges - Tech extra work/Flowers/Flowers.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading