Skip to content

Commit eed9871

Browse files
authored
Punched cards JS solution.
1 parent 81beb98 commit eed9871

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

punchedcards.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
3+
'use strict';
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', inputStdin => {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', _ => {
16+
inputString = inputString.trim().split('\n').map(string => {
17+
return string.trim();
18+
});
19+
20+
main();
21+
});
22+
23+
function readline() {
24+
return inputString[currentLine++];
25+
}
26+
//----------------------------------------------------------------------
27+
28+
29+
// This function draws the card saving it in a variable "card".
30+
function punched_cards(R, C) {
31+
32+
// We initiate by creating the top row, which will always be there.
33+
// The following code types the top edge depending on the # of columns.
34+
let row = "..+";
35+
for (let j = 0; j < (C-1); j++) {
36+
row += "-+";
37+
}
38+
row += "\n";
39+
40+
let card = row;
41+
42+
// Now, we create each new row, starting with an exception for the first iteration.
43+
// This is because the top left corner of the card is only dots.
44+
row = "..|";
45+
let row2 = "+-+";
46+
for (let j = 0; j < R; j++) {
47+
for (let k = 0; k < (C-1); k++) {
48+
row += ".|";
49+
row2 += "-+";
50+
}
51+
row += "\n";
52+
row2 += "\n"
53+
card += row + row2
54+
55+
// With this resets, all following iterations will only have "+-+", unlike the top left corner.
56+
row = "|.|";
57+
row2 = "+-+";
58+
}
59+
60+
return card;
61+
}
62+
63+
// This function receives the values for R and C in each iteration from all the T cases.
64+
// It uses those values to draw the punched card using the punched_cards function and then
65+
// gives it as output.
66+
function solve() {
67+
// Declare variables R and C.
68+
var R, C;
69+
// Read the integers from the standard input.
70+
[R, C] = readline().split(' ').map(x => parseInt(x));
71+
72+
// Obtains the card from the punched_cards function.
73+
var card = punched_cards(R, C);
74+
75+
// Print the result onto the standard output.
76+
process.stdout.write(card);
77+
}
78+
79+
80+
function main() {
81+
// Declare and read the number of test cases.
82+
var T;
83+
T = parseInt(readline());
84+
85+
// Loop over the number of test cases.
86+
for (var test_no = 1; test_no <= T; test_no++) {
87+
process.stdout.write('Case #' + test_no + ': \n');
88+
solve();
89+
}
90+
}

0 commit comments

Comments
 (0)