Skip to content

Commit a8914fb

Browse files
Add files via upload
1 parent 0c23bf9 commit a8914fb

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

21 - Generics/Solution.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
template<typename Element>
7+
8+
void printArray(vector<Element> arr) {
9+
for (int i = 0; i < arr.size(); i++)
10+
cout << arr[i] << endl;
11+
}
12+
//Aditya Seth
13+
14+
int main() {
15+
vector<int> vInt{1, 2, 3};
16+
vector<string> vString{"Hello", "World"};
17+
18+
printArray<int>(vInt);
19+
printArray<string>(vString);
20+
21+
return 0;
22+
}

21 - Generics/Solution.kt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
internal class PrinterKotlin<T> {
2+
fun printArray(array: Array<T>) {
3+
for (t in array) {
4+
println(t)
5+
}
6+
}
7+
}
8+
9+
fun main(args: Array<String>) {
10+
val scanner = Scanner(System.`in`)
11+
var n = scanner.nextInt()
12+
val intArray = Array(n) { 0 }
13+
for (i in 0 until n) {
14+
intArray[i] = scanner.nextInt()
15+
}
16+
n = scanner.nextInt()
17+
val stringArray = Array(n) { "" }
18+
for (i in 0 until n) {
19+
stringArray[i] = scanner.next()
20+
}
21+
val intPrinter = PrinterKotlin<Int>()
22+
val stringPrinter = PrinterKotlin<String>()
23+
intPrinter.printArray(intArray)
24+
stringPrinter.printArray(stringArray)
25+
if (PrinterKotlin::class.java.declaredMethods.size > 1) {
26+
println("The Printer class should only have 1 method named printArray.")
27+
}
28+
}

21 - Generics/Solution.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import TypeVar
2+
3+
Element = TypeVar("Element")
4+
5+
6+
def printArray(array: [Element]):
7+
for element in array:
8+
print(element)
9+
#Aditya Seth
10+
11+
vInt = [1, 2, 3]
12+
vString = ["Hello", "World"]
13+
14+
printArray(vInt)
15+
printArray(vString)

0 commit comments

Comments
 (0)