Last active
August 29, 2015 14:20
-
-
Save nstanevski/ccbe474e87b16dadc808 to your computer and use it in GitHub Desktop.
Homework#1 - Arrays, Lists, Stacks, Ques
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
/* | |
*Write a program to sort an array of numbers and then print them back on the console. | |
*The numbers should be entered from the console on a single line, separated by a space. | |
*/ | |
class SortArrayOfNumbers | |
{ | |
static void Main() | |
{ | |
int[] numArr = Console.ReadLine().Trim().Split().Select(p => int.Parse(p)).OrderBy(p => p).ToArray(); | |
Console.WriteLine(string.Join(" ", numArr)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
class SortArrayUsingSelectionSort | |
{ | |
private static void selectionSort(int[] arr) | |
{ | |
int minPos = 0, min = 0; | |
for (int i = 0; i < arr.Length - 1; i++) | |
{ | |
min = arr[i]; | |
minPos = i; | |
for (int j = i + 1; j < arr.Length; j++) | |
{ | |
if (arr[j] < min) | |
{ | |
min = arr[j]; | |
minPos = j; | |
} | |
} | |
if (minPos != i) | |
{ | |
int buf = arr[i]; | |
arr[i] = arr[minPos]; | |
arr[minPos] = buf; | |
} | |
} | |
} | |
static void Main() | |
{ | |
string[] strArr = Console.ReadLine().Split(); | |
int[] numArr = Array.ConvertAll(strArr, s => int.Parse(s)); | |
selectionSort(numArr); | |
Console.WriteLine(string.Join(" ", numArr)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
/* | |
* Write a program that reads N floating-point numbers from the console. | |
* Your task is to separate them in two sets, one containing only the round numbers | |
* (e.g. 1, 1.00, etc.) and the other containing the floating-point numbers | |
* with non-zero fraction. Print both arrays along with their minimum, maximum, | |
* sum and average (rounded to two decimal places). | |
*/ | |
class CategorizeNumbers | |
{ | |
static void Main() | |
{ | |
string[] strArr = Console.ReadLine().Split(); | |
double[] doubleArr = Array.ConvertAll(strArr, s => double.Parse(s)); | |
const double EPS = 1e-14; | |
List<double> doubleList = new List<double>(); | |
List<int> intList = new List<int>(); | |
foreach (double item in doubleArr) | |
{ | |
if(Math.Abs(item - (int) item) <= EPS) | |
{ | |
intList.Add((int) item); | |
} | |
else | |
{ | |
doubleList.Add(item); | |
} | |
} | |
double doubleMin = doubleList.Min(); | |
double doubleMax = doubleList.Max(); | |
double doubleSum = doubleList.Sum(); | |
double doubleAvg = doubleList.Average(); | |
string doubleListStr = "[" + string.Join(", ", doubleList) + "]"; | |
int intMin = intList.Min(); | |
int intMax = intList.Max(); | |
int intSum = intList.Sum(); | |
double intAvg = intList.Average(); | |
string intListStr = "[" + string.Join(", ", intList)+"]"; | |
Console.WriteLine("{0} -> min: {1:#.###}, max: {2:#.###}, sum: {3:#.###}, avg: {4:#.###}", | |
doubleListStr, doubleMin, doubleMax, doubleSum, doubleAvg); | |
Console.WriteLine("{0} -> min: {1}, max: {2}, sum: {3}, avg: {4:#.###}", | |
intListStr, intMin, intMax, intSum, intAvg); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Write a program that reads an array of strings and finds in it all sequences | |
* of equal elements (comparison should be case-insensitive). | |
* The input strings are given as a single line, separated by a space. | |
*/ | |
class SequenceОfEqualStrings | |
{ | |
static void Main() | |
{ | |
string[] stringArr = Console.ReadLine().Split(); | |
for (int i = 0; i < stringArr.Length - 1; i++) | |
{ | |
Console.Write("{0} ", stringArr[i]); | |
if(!stringArr[i].Equals(stringArr[i+1])){ | |
Console.WriteLine(); | |
} | |
} | |
Console.WriteLine(stringArr[stringArr.Length - 1]); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
/* | |
* Write a program to find all increasing sequences inside an array of integers. | |
* The integers are given on a single line, separated by a space. | |
* Print the sequences in the order of their appearance in the input array, | |
* each at a single line. Separate the sequence elements by a space. | |
* Find also the longest increasing sequence and print it at the last line. | |
* If several sequences have the same longest length, print the left-most of them. | |
*/ | |
class LongestIncreasingSequence | |
{ | |
static void Main() | |
{ | |
string[] strArr = Console.ReadLine().Split(); | |
int[] numArr = Array.ConvertAll(strArr, s => int.Parse(s)); | |
List<List<int>> sequencesList = new List<List<int>>(); | |
List<int> sequence = new List<int>(); | |
//build the list of lists of increasing sequences | |
for (int i = 0; i < numArr.Length - 1; i++) | |
{ | |
sequence.Add(numArr[i]); | |
if (numArr[i] >= numArr[i + 1]) | |
{ | |
sequencesList.Add(sequence); | |
sequence = new List<int>(); | |
} | |
} | |
//process the last element of the input array | |
if (numArr[numArr.Length - 1] > numArr[numArr.Length - 2]) | |
{ | |
sequence.Add(numArr[numArr.Length - 1]); | |
sequencesList.Add(sequence); | |
} | |
else | |
{ | |
if(sequence.Count >0) | |
sequencesList.Add(sequence); | |
sequence = new List<int>(); | |
sequence.Add(numArr[numArr.Length - 1]); | |
sequencesList.Add(sequence); | |
} | |
int maxCount = 0, maxIndex = 0; | |
//print the sequences | |
foreach (List<int> seq in sequencesList) | |
{ | |
if (seq.Count > maxCount) | |
{ | |
maxCount = seq.Count; | |
maxIndex = sequencesList.IndexOf(seq); | |
} | |
Console.WriteLine(string.Join(" ", seq)); | |
} | |
//print the longest sequence | |
List<int> longestSeq = sequencesList[maxIndex]; | |
Console.WriteLine("Longest: " + string.Join(" ", longestSeq)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
/* | |
* Write a program that reads from the console a number N and an array of integers | |
* given on a single line. Your task is to find all subsets within the array | |
* which have a sum equal to N and print them on the console | |
* (the order of printing is not important). | |
* Find only the unique subsets by filtering out repeating numbers first. | |
* In case there aren't any subsets with the desired sum, print "No matching subsets." | |
*/ | |
class SubsetSums | |
{ | |
//returns list with positions of digit '1' at the binary | |
//representation of number num. Positions are counted from | |
//right to left. Rightmost is position 0. | |
//Sample: 5 is 101 and the result is [0, 2] | |
private static List<int> getOnePositions(int num) | |
{ | |
List<int> result = new List<int>(); | |
int i = 0; | |
while (num > 0) | |
{ | |
if (num % 2 == 1) | |
result.Add(i); | |
num /= 2; | |
i++; | |
} | |
return result; | |
} | |
static void Main() | |
{ | |
int sum = int.Parse(Console.ReadLine()); | |
string[] arrStr = Console.ReadLine().Split(); | |
//filter repeating numbers | |
List<int> numbers = new List<int>(); | |
foreach(string str in arrStr){ | |
int num = int.Parse(str); | |
if(numbers.IndexOf(num) == -1) | |
numbers.Add(num); | |
} | |
bool found = false; | |
int listCount = numbers.Count; | |
//number of possible combinations of elements of numbers list: | |
int numCombinations = (int)Math.Pow(2,listCount) - 1; | |
List<int> indexes; | |
for(int i=0; i<numCombinations; i++){ | |
indexes = getOnePositions(i); | |
int currentSum = 0; | |
foreach (int index in indexes) | |
currentSum += numbers[index]; | |
if (currentSum == sum) | |
{ | |
List<int> numbersUsed = new List<int>(); | |
foreach (int index in indexes) | |
numbersUsed.Add(numbers[index]); | |
if (numbersUsed.Count > 0) | |
{ | |
found = true; | |
Console.WriteLine(string.Join(" + ", numbersUsed) + " = " + sum); | |
} | |
} | |
} | |
if (!found) | |
Console.WriteLine("No matching subsets."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class LegoBlocks | |
{ | |
static void Main() | |
{ | |
int count = int.Parse(Console.ReadLine()); | |
List<int>[] firstArray = new List<int>[count]; | |
List<int>[] secondArray = new List<int>[count]; | |
char[] separators = new char[] { ' ', '\t' }; | |
//fill jagged arrays: | |
for (int i = 0; i < count; i++) | |
{ | |
List<int> row = Console.ReadLine().Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); | |
firstArray[i] = row; | |
} | |
for (int i = 0; i < count; i++) | |
{ | |
List<int> row = Console.ReadLine().Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); | |
secondArray[i] = row; | |
} | |
//reverse second jagged array: | |
for (int i = 0; i < count; i++) | |
{ | |
secondArray[i].Reverse(); | |
} | |
//check for fitting | |
int firstRowSize = firstArray[0].Count + secondArray[0].Count; | |
bool fit = true; | |
for (int i = 1; i < count; i++) | |
{ | |
int rowSize = firstArray[i].Count + secondArray[i].Count; | |
if (rowSize != firstRowSize) | |
{ | |
fit = false; | |
break; | |
} | |
} | |
if (!fit) | |
{ | |
int cellsCount = 0; | |
for (int i = 0; i < count; i++) | |
{ | |
cellsCount += firstArray[i].Count; | |
cellsCount += secondArray[i].Count; | |
} | |
Console.WriteLine("The total number of cells is: "+cellsCount); | |
} | |
else | |
{ | |
for (int i = 0; i < count; i++) | |
{ | |
string rowStr = "[" + string.Join(", ", firstArray[i]) + ", "; | |
rowStr += string.Join(", ", secondArray[i]) + "]"; | |
Console.WriteLine(rowStr); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
/* | |
* You are given n numbers. Write a program to find among these numbers | |
* all sets of 4 numbers {a, b, c, d}, such that a|b == c|d, where a ≠ b ≠ c ≠ d. | |
* Assume that "a|b" means to append the number b after a. | |
* We call these numbers {a, b, c, d} stuck numbers: | |
* if we append a and b, we get the same result as if we appended c and d. | |
* Note that the numbers a, b, c and d should be distinct (a ≠ b ≠ c ≠ d). | |
*/ | |
class StuckNumbers | |
{ | |
static void Main() | |
{ | |
int count = int.Parse(Console.ReadLine()); | |
int[] numbers = Console.ReadLine().Trim().Split().Select(int.Parse).ToArray(); | |
int a=0, b=0, c=0, d=0; | |
bool found = false; | |
for (int i = 0; i < count; i++) | |
{ | |
for (int j = 0; j < count; j++) | |
{ | |
for (int k = 0; k < count; k++) | |
{ | |
for (int l = 0; l < count; l++) | |
{ | |
if( i!= j && i != k && i != l && j!= k && j != l && k != l) | |
{ | |
a = numbers[i]; b = numbers[j]; c = numbers[k]; d = numbers[l]; | |
string s1 = a.ToString() + b.ToString(); | |
string s2 = c.ToString() + d.ToString(); | |
if (s1.Equals(s2)) | |
{ | |
Console.WriteLine("{0}|{1}=={2}|{3}", a, b, c, d); | |
found = true; | |
} | |
} | |
} | |
} | |
} | |
} | |
if (!found) | |
Console.WriteLine("No"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class PythagoreanNumbers | |
{ | |
static void Main() | |
{ | |
int count = int.Parse(Console.ReadLine()); | |
int[] numArr = new int[count]; | |
for (int n = 0; n < count; n++) | |
numArr[n] = int.Parse(Console.ReadLine()); | |
bool found = false; | |
for (int i = 0; i < count; i++) | |
{ | |
for (int j = 0; j < count; j++) | |
{ | |
for (int k = 0; k < count; k++) | |
{ | |
if (numArr[i] * numArr[i] + numArr[j] * numArr[j] == numArr[k] * numArr[k] | |
&& | |
numArr [i] <= numArr[j] | |
) | |
{ | |
found = true; | |
Console.WriteLine("{0}*{0} + {1}*{1} = {2}*{2}", numArr[i], numArr[j], numArr[k]); | |
} | |
} | |
} | |
} | |
if (!found) | |
Console.WriteLine("No"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment