Commit 8c54737760cc7490737daeb6c4951d3ef39957c0

Authored by Jordi Inglada
1 parent cc405661
Exists in master

np where

Showing 1 changed file with 188 additions and 0 deletions   Show diff stats
scrapbook.org
... ... @@ -1288,3 +1288,191 @@ print [i*16+2 for i in range(30) if (i*16+2)/5==(i*16+2)/5.0]
1288 1288 : [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 310, 315, 320, 325, 330, 335, 340, 345, 350, 355, 360, 365, 370]
1289 1289 : [50, 130, 210, 290, 370, 450]
1290 1290  
  1291 +* random chars
  1292 +#+begin_src C++
  1293 +#include <string>
  1294 +#include <cstdlib>
  1295 +#include <ctime>
  1296 +#include <iostream>
  1297 +int main()
  1298 +{
  1299 + std::srand(std::time(0));
  1300 + auto c1 = char(std::rand()%('Z'-'A'+1)+'A');
  1301 + auto c2 = char(std::rand()%('Z'-'A'+1)+'A');
  1302 + auto n = std::to_string(std::rand()%1000);
  1303 +n += std::string(n.size()<3?"0":"")+std::string(n.size()<2?"0":"");
  1304 + std::string name = std::string{c1}+c2+n;
  1305 + std::cout << name <<"\n";
  1306 +}
  1307 +#+end_src
  1308 +
  1309 +#+RESULTS:
  1310 +: OU892
  1311 +
  1312 +* Modulos
  1313 +#+begin_src C++
  1314 +#include <iostream>
  1315 +int main()
  1316 +{
  1317 + std::cout << (15-27)%60 <<"\n";
  1318 +}
  1319 +#+end_src
  1320 +
  1321 +#+RESULTS:
  1322 +: -12
  1323 +
  1324 +* TC
  1325 +
  1326 +** Hex
  1327 +
  1328 +Problem Statement
  1329 +
  1330 +#+BEGIN_EXAMPLE
  1331 +_
  1332 +/h\_
  1333 +\_/ \_
  1334 +/v\_/ \_
  1335 +\_/ \_/ \
  1336 +/v\_/h\_/
  1337 +\_/ \_/ \
  1338 +/v\_/ \_/
  1339 +\_/ \_/ \
  1340 +\_/ \_/
  1341 +\_/ \
  1342 +\_/
  1343 +
  1344 +Above is a picture of a 4 x 4 Hex game in progress. The board is a 4 x 4 collection of hexagons packed together, with 4 hexagons in each vertical column, and 4 hexagons in each diagonal from upper left to lower right. Two players play against each other. One of the players ('h') tries to make a horizontal chain of adjacent hexagons stretching between the left and right of the board. The other player ('v') tries to make a vertical chain of adjacent hexagons stretching from the bottom to the top of the board.
  1345 +
  1346 +We can refer to any position on the board by a pair of coordinates giving the diagonal distance and vertical distance from the upper left hexagon. Using these coordinates, the two hexagons marked 'h' are located at (0,0) and at (2,1).
  1347 + Given the size of the board and a list of all the marked hexagons, we want software that can draw the board using characters as shown above. Create a class Hex that contains a method picture that is given n (the vertical and diagonal size of the board) and marks (a list of all the marked hexagons) and that returns a picture of the board in the format shown above. The return will be a that if printed one per line in order would produce the picture. Each element of the return should have no trailing spaces, and at least one of the elements should have no leading spaces.
  1348 +
  1349 +marks will be a in which each element will consist of exactly 3 characters: two digits giving the diagonal and then the vertical coordinate of a hexagon, followed by either 'v' or 'h', the marking of that hexagon.
  1350 +Definition
  1351 +Class:
  1352 +Hex
  1353 +Method:
  1354 +picture
  1355 +Parameters:
  1356 +int, String[]
  1357 +Returns:
  1358 +String[]
  1359 +Method signature:
  1360 +String[] picture(int n, String[] marks)
  1361 +(be sure your method is public)
  1362 +Limits
  1363 +Time limit (s):
  1364 +840.000
  1365 +Memory limit (MB):
  1366 +64
  1367 +
  1368 +
  1369 +Constraints
  1370 +- n will be between 2 and 10 inclusive.
  1371 +- marks will contain between 0 and 50 elements inclusive.
  1372 +- Each element of marks will contain exactly 3 characters.
  1373 +- The first 2 characters in each element of marks will be digits less than n.
  1374 +- The last character in each element of marks will be 'v' or 'h'.
  1375 +- No 2 elements of marks will refer to the same hexagon.
  1376 +Examples
  1377 +0)
  1378 +4
  1379 +{"00h","21h","01v","03v","02v"}
  1380 +Returns: { " _", "/h\\_", "\\_/ \\_", "/v\\_/ \\_", "\\_/ \\_/ \\", "/v\\_/h\\_/", "\\_/ \\_/ \\", "/v\\_/ \\_/", "\\_/ \\_/ \\", " \\_/ \\_/", " \\_/ \\", " \\_/" }
  1381 +Note that the elements in the returns are shown as string literals, so each backslash character in each is shown as \\. So, for example, the second element of this return should contain just 4 characters (i.e. its length would be 4). This will print the following 4x4 picture:
  1382 + _
  1383 +/h\_
  1384 +\_/ \_
  1385 +/v\_/ \_
  1386 +\_/ \_/ \
  1387 +/v\_/h\_/
  1388 +\_/ \_/ \
  1389 +/v\_/ \_/
  1390 +\_/ \_/ \
  1391 +\_/ \_/
  1392 +\_/ \
  1393 +\_/
  1394 +
  1395 +1)
  1396 +3
  1397 +{"00v","01v","02v","11h","21h"}
  1398 +Returns: { " _", "/v\\_", "\\_/ \\_", "/v\\_/ \\", "\\_/h\\_/", "/v\\_/h\\", "\\_/ \\_/", " \\_/ \\", " \\_/" }
  1399 +This will print the following 3x3 picture:
  1400 +
  1401 +_
  1402 +/v\_
  1403 +\_/ \_
  1404 +/v\_/ \
  1405 +\_/h\_/
  1406 +/v\_/h\
  1407 +\_/ \_/
  1408 +\_/ \
  1409 +\_/
  1410 +#+END_EXAMPLE
  1411 +
  1412 +#+begin_src C++ :results verbatim
  1413 +#include <string>
  1414 +#include <vector>
  1415 +#include <iostream>
  1416 +
  1417 +using PictureType = std::vector<std::string>;
  1418 +using EncodingType = std::vector<std::string>;
  1419 +void print_picture(PictureType p)
  1420 +{
  1421 + for(const auto& l : p)
  1422 + std::cout << l <<"\n";
  1423 +}
  1424 +
  1425 +struct Hex
  1426 +{
  1427 +static PictureType picture(int n, EncodingType marks)
  1428 + {
  1429 + return marks;
  1430 + }
  1431 +};
  1432 +int main()
  1433 +{
  1434 + EncodingType e{"00h","21h","01v","03v","02v"};
  1435 + PictureType p{ " _", "/h\\_", "\\_/ \\_", "/v\\_/ \\_", "\\_/ \\_/ \\",
  1436 + "/v\\_/h\\_/", "\\_/ \\_/ \\", "/v\\_/ \\_/", "\\_/ \\_/ \\",
  1437 + " \\_/ \\_/", " \\_/ \\", " \\_/" };
  1438 + print_picture(p);
  1439 + auto pp = Hex::picture(4,e);
  1440 +}
  1441 +#+end_src
  1442 +
  1443 +#+RESULTS:
  1444 +#+begin_example
  1445 +_
  1446 +/h\_
  1447 +\_/ \_
  1448 +/v\_/ \_
  1449 +\_/ \_/ \
  1450 +/v\_/h\_/
  1451 +\_/ \_/ \
  1452 +/v\_/ \_/
  1453 +\_/ \_/ \
  1454 + \_/ \_/
  1455 + \_/ \
  1456 + \_/
  1457 +#+end_example
  1458 +
  1459 +* np.where
  1460 +#+BEGIN_SRC python :results output
  1461 +import numpy as np
  1462 +
  1463 +
  1464 +x = np.arange(9.).reshape(3,3)
  1465 +ids = [1,3,5]
  1466 +print x
  1467 +y = np.where(reduce(np.logical_or,map(lambda id: np.where(x == id, True, False), ids)),x,0)
  1468 +print y
  1469 +#+END_SRC
  1470 +
  1471 +#+RESULTS:
  1472 +: [[ 0. 1. 2.]
  1473 +: [ 3. 4. 5.]
  1474 +: [ 6. 7. 8.]]
  1475 +: [[ 0. 1. 0.]
  1476 +: [ 3. 0. 5.]
  1477 +: [ 0. 0. 0.]]
  1478 +
... ...