質問編集履歴

1

質問点を明確化

2017/02/14 21:41

投稿

Surface-Yuki
Surface-Yuki

スコア34

test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- このエラーによると、12行目のstdio.write(sys.argv[1])に問題があるようなのですが、
31
+ [質問点1]このエラーによると、12行目のstdio.write(sys.argv[1])に問題があるようなのですが、
32
32
 
33
33
  どのように修正すれば良いのか教えてください。
34
34
 
@@ -44,6 +44,28 @@
44
44
 
45
45
  import sys となっているのに関わらず、sys.pyというファイルが見つからないのが不思議です。
46
46
 
47
+ 全データ検索で探しましたが見つかりません。
48
+
49
+ sys.stdin.pyという名前の似ているファイルは見つかりました。
50
+
51
+
52
+
53
+
54
+
55
+ [質問点2]コード内の
56
+
57
+ stdio.write(sys.argv[1])
58
+
59
+
60
+
61
+ というのはどのような命令なのでしょうか?
62
+
63
+ import でstdioがすでに紐づけされているので、その中のwrite という命令を行っている
64
+
65
+ ということでしょうか?
66
+
67
+
68
+
47
69
 
48
70
 
49
71
  ![イメージ説明](01c7f617ef5fae213a5364947be93cfa.png)
@@ -246,75 +268,349 @@
246
268
 
247
269
 
248
270
 
271
+ 省略
272
+
273
+ #-----------------------------------------------------------------------
274
+
275
+
276
+
249
- def _readRegExp(regExp):
277
+ def isEmpty():
250
-
278
+
251
- """
279
+ """
252
-
253
- Discard leading white space characters from standard input. Then read
280
+
254
-
255
- from standard input and return a string matching regular expression
256
-
257
- regExp. Raise an EOFError if no non-whitespace characters remain
281
+ Return True if no non-whitespace characters remain in standard
258
-
282
+
259
- in standard input. Raise a ValueError if the next characters to
283
+ input. Otherwise return False.
260
-
261
- be read from standard input do not match 'regExp'.
262
284
 
263
285
  """
264
286
 
265
287
  global _buffer
266
288
 
289
+ while _buffer.strip() == '':
290
+
291
+ line = sys.stdin.readline()
292
+
293
+ if sys.hexversion < 0x03000000:
294
+
295
+ line = line.decode('utf-8')
296
+
297
+ if line == '':
298
+
299
+ return True
300
+
301
+ _buffer += line
302
+
303
+ return False
304
+
305
+
306
+
307
+ #-----------------------------------------------------------------------
308
+
309
+
310
+
311
+ 省略
312
+
313
+
314
+
315
+ #-----------------------------------------------------------------------
316
+
317
+
318
+
319
+ def readAllInts():
320
+
321
+ """
322
+
323
+ Read all remaining strings from standard input, convert each to
324
+
325
+ an int, and return those ints in an array. Raise a ValueError if
326
+
327
+ any of the strings cannot be converted to an int.
328
+
329
+ """
330
+
331
+ strings = readAllStrings()
332
+
333
+ ints = []
334
+
335
+ for s in strings:
336
+
337
+ i = int(s)
338
+
339
+ ints.append(i)
340
+
341
+ return ints
342
+
343
+
344
+
345
+ #-----------------------------------------------------------------------
346
+
347
+
348
+
349
+ def readFloat():
350
+
351
+ """
352
+
353
+ Discard leading white space characters from standard input. Then
354
+
355
+ read from standard input a sequence of characters comprising a
356
+
357
+ float. Convert the sequence of characters to a float, and return the
358
+
359
+ float. Raise an EOFError if no non-whitespace characters remain
360
+
361
+ in standard input. Raise a ValueError if the next characters to be
362
+
363
+ read from standard input cannot comprise a float.
364
+
365
+ """
366
+
367
+ s = _readRegExp(r'[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?')
368
+
369
+ return float(s)
370
+
371
+
372
+
373
+ #-----------------------------------------------------------------------
374
+
375
+
376
+
377
+ def readAllFloats():
378
+
379
+ """
380
+
381
+ Read all remaining strings from standard input, convert each to
382
+
383
+ a float, and return those floats in an array. Raise a ValueError if
384
+
385
+ any of the strings cannot be converted to a float.
386
+
387
+ """
388
+
389
+ strings = readAllStrings()
390
+
391
+ floats = []
392
+
393
+ for s in strings:
394
+
395
+ f = float(s)
396
+
397
+ floats.append(f)
398
+
399
+ return floats
400
+
401
+
402
+
403
+ #-----------------------------------------------------------------------
404
+
405
+
406
+
407
+ def readBool():
408
+
409
+ """
410
+
411
+ Discard leading white space characters from standard input. Then
412
+
413
+ read from standard input a sequence of characters comprising a bool.
414
+
415
+ Convert the sequence of characters to a bool, and return the
416
+
417
+ bool. Raise an EOFError if no non-whitespace characters remain
418
+
419
+ in standard input. Raise a ValueError if the next characters to be
420
+
421
+ read from standard input cannot comprise a bool.
422
+
423
+
424
+
425
+ These character sequences can comprise a bool:
426
+
427
+ -- True
428
+
429
+ -- False
430
+
431
+ -- 1 (means true)
432
+
433
+ -- 0 (means false)
434
+
435
+ """
436
+
437
+ s = _readRegExp(r'(True)|(False)|1|0')
438
+
439
+ if (s == 'True') or (s == '1'):
440
+
441
+ return True
442
+
443
+ return False
444
+
445
+
446
+
447
+ #-----------------------------------------------------------------------
448
+
449
+
450
+
451
+ def readAllBools():
452
+
453
+ """
454
+
455
+ Read all remaining strings from standard input, convert each to
456
+
457
+ a bool, and return those bools in an array. Raise a ValueError if
458
+
459
+ any of the strings cannot be converted to a bool.
460
+
461
+ """
462
+
463
+ strings = readAllStrings()
464
+
465
+ bools = []
466
+
467
+ for s in strings:
468
+
469
+ b = bool(s)
470
+
471
+ bools.append(b)
472
+
473
+ return bools
474
+
475
+
476
+
477
+ #-----------------------------------------------------------------------
478
+
479
+
480
+
481
+ def readString():
482
+
483
+ """
484
+
485
+ Discard leading white space characters from standard input. Then
486
+
487
+ read from standard input a sequence of characters comprising a
488
+
489
+ string, and return the string. Raise an EOFError if no
490
+
491
+ non-whitespace characters remain in standard input.
492
+
493
+ """
494
+
495
+ s = _readRegExp(r'\S+')
496
+
497
+ return s
498
+
499
+
500
+
501
+ #-----------------------------------------------------------------------
502
+
503
+
504
+
505
+ def readAllStrings():
506
+
507
+ """
508
+
509
+ Read all remaining strings from standard input, and return them in
510
+
511
+ an array.
512
+
513
+ """
514
+
515
+ strings = []
516
+
267
- if isEmpty():
517
+ while not isEmpty():
518
+
519
+ s = readString()
520
+
521
+ strings.append(s)
522
+
523
+ return strings
524
+
525
+
526
+
527
+ #-----------------------------------------------------------------------
528
+
529
+
530
+
531
+ def hasNextLine():
532
+
533
+ """
534
+
535
+ Return True if standard input has a next line. Otherwise return
536
+
537
+ False.
538
+
539
+ """
540
+
541
+ global _buffer
542
+
543
+ if _buffer != '':
544
+
545
+ return True
546
+
547
+ else:
548
+
549
+ _buffer = sys.stdin.readline()
550
+
551
+ if sys.hexversion < 0x03000000:
552
+
553
+ _buffer = _buffer.decode('utf-8')
554
+
555
+ if _buffer == '':
556
+
557
+ return False
558
+
559
+ return True
560
+
561
+
562
+
563
+ #-----------------------------------------------------------------------
564
+
565
+
566
+
567
+ def readLine():
568
+
569
+ """
570
+
571
+ Read and return as a string the next line of standard input.
572
+
573
+ Raise an EOFError is there is no next line.
574
+
575
+ """
576
+
577
+ global _buffer
578
+
579
+ if not hasNextLine():
268
580
 
269
581
  raise EOFError()
270
582
 
271
- compiledRegExp = re.compile(r'^\s*' + regExp)
272
-
273
- match = compiledRegExp.search(_buffer)
274
-
275
- if match is None:
276
-
277
- raise ValueError()
278
-
279
- s = match.group()
583
+ s = _buffer
280
-
584
+
281
- _buffer = _buffer[match.end():]
585
+ _buffer = ''
282
-
586
+
283
- return s.lstrip()
587
+ return s.rstrip('\n')
284
-
285
-
286
-
588
+
589
+
590
+
287
- #-----------------------------------------------------------------------
591
+ #-----------------------------------------------------------------------
288
-
289
-
290
-
592
+
593
+
594
+
291
- def isEmpty():
595
+ def readAllLines():
292
-
596
+
293
- """
597
+ """
294
-
598
+
295
- Return True if no non-whitespace characters remain in standard
599
+ Read all remaining lines from standard input, and return them as
296
-
600
+
297
- input. Otherwise return False.
601
+ strings in an array.
298
-
602
+
299
- """
603
+ """
300
-
604
+
301
- global _buffer
605
+ lines = []
302
-
606
+
303
- while _buffer.strip() == '':
607
+ while hasNextLine():
304
-
608
+
305
- line = sys.stdin.readline()
609
+ line = readLine()
306
-
307
- if sys.hexversion < 0x03000000:
610
+
308
-
309
- line = line.decode('utf-8')
310
-
311
- if line == '':
611
+ lines.append(line)
312
-
612
+
313
- return True
613
+ return lines
314
-
315
- _buffer += line
316
-
317
- return False
318
614
 
319
615
 
320
616
 
@@ -326,305 +622,41 @@
326
622
 
327
623
 
328
624
 
329
- #-----------------------------------------------------------------------
330
-
331
-
332
-
333
- def readAllInts():
334
-
335
- """
336
-
337
- Read all remaining strings from standard input, convert each to
338
-
339
- an int, and return those ints in an array. Raise a ValueError if
340
-
341
- any of the strings cannot be converted to an int.
342
-
343
- """
344
-
345
- strings = readAllStrings()
346
-
347
- ints = []
348
-
349
- for s in strings:
350
-
351
- i = int(s)
352
-
353
- ints.append(i)
354
-
355
- return ints
356
-
357
-
358
-
359
- #-----------------------------------------------------------------------
360
-
361
-
362
-
363
- def readFloat():
364
-
365
- """
366
-
367
- Discard leading white space characters from standard input. Then
368
-
369
- read from standard input a sequence of characters comprising a
370
-
371
- float. Convert the sequence of characters to a float, and return the
372
-
373
- float. Raise an EOFError if no non-whitespace characters remain
374
-
375
- in standard input. Raise a ValueError if the next characters to be
376
-
377
- read from standard input cannot comprise a float.
378
-
379
- """
380
-
381
- s = _readRegExp(r'[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?')
382
-
383
- return float(s)
384
-
385
-
386
-
387
- #-----------------------------------------------------------------------
388
-
389
-
390
-
391
- def readAllFloats():
392
-
393
- """
394
-
395
- Read all remaining strings from standard input, convert each to
396
-
397
- a float, and return those floats in an array. Raise a ValueError if
398
-
399
- any of the strings cannot be converted to a float.
400
-
401
- """
402
-
403
- strings = readAllStrings()
404
-
405
- floats = []
406
-
407
- for s in strings:
408
-
409
- f = float(s)
410
-
411
- floats.append(f)
412
-
413
- return floats
414
-
415
-
416
-
417
- #-----------------------------------------------------------------------
418
-
419
-
420
-
421
- def readBool():
422
-
423
- """
424
-
425
- Discard leading white space characters from standard input. Then
426
-
427
- read from standard input a sequence of characters comprising a bool.
428
-
429
- Convert the sequence of characters to a bool, and return the
430
-
431
- bool. Raise an EOFError if no non-whitespace characters remain
432
-
433
- in standard input. Raise a ValueError if the next characters to be
434
-
435
- read from standard input cannot comprise a bool.
436
-
437
-
438
-
439
- These character sequences can comprise a bool:
440
-
441
- -- True
442
-
443
- -- False
444
-
445
- -- 1 (means true)
446
-
447
- -- 0 (means false)
448
-
449
- """
450
-
451
- s = _readRegExp(r'(True)|(False)|1|0')
452
-
453
- if (s == 'True') or (s == '1'):
454
-
455
- return True
456
-
457
- return False
458
-
459
-
460
-
461
- #-----------------------------------------------------------------------
462
-
463
-
464
-
465
- def readAllBools():
466
-
467
- """
468
-
469
- Read all remaining strings from standard input, convert each to
470
-
471
- a bool, and return those bools in an array. Raise a ValueError if
472
-
473
- any of the strings cannot be converted to a bool.
474
-
475
- """
476
-
477
- strings = readAllStrings()
478
-
479
- bools = []
480
-
481
- for s in strings:
482
-
483
- b = bool(s)
484
-
485
- bools.append(b)
486
-
487
- return bools
488
-
489
-
490
-
491
- #-----------------------------------------------------------------------
492
-
493
-
494
-
495
- def readString():
496
-
497
- """
498
-
499
- Discard leading white space characters from standard input. Then
500
-
501
- read from standard input a sequence of characters comprising a
502
-
503
- string, and return the string. Raise an EOFError if no
504
-
505
- non-whitespace characters remain in standard input.
506
-
507
- """
508
-
509
- s = _readRegExp(r'\S+')
510
-
511
- return s
512
-
513
-
514
-
515
- #-----------------------------------------------------------------------
516
-
517
-
518
-
519
- def readAllStrings():
520
-
521
- """
522
-
523
- Read all remaining strings from standard input, and return them in
524
-
525
- an array.
526
-
527
- """
528
-
529
- strings = []
530
-
531
- while not isEmpty():
532
-
533
- s = readString()
534
-
535
- strings.append(s)
536
-
537
- return strings
538
-
539
-
540
-
541
- #-----------------------------------------------------------------------
542
-
543
-
544
-
545
- def hasNextLine():
546
-
547
- """
548
-
549
- Return True if standard input has a next line. Otherwise return
550
-
551
- False.
552
-
553
- """
554
-
555
- global _buffer
556
-
557
- if _buffer != '':
558
-
559
- return True
560
-
561
- else:
562
-
563
- _buffer = sys.stdin.readline()
564
-
565
- if sys.hexversion < 0x03000000:
566
-
567
- _buffer = _buffer.decode('utf-8')
568
-
569
- if _buffer == '':
570
-
571
- return False
572
-
573
- return True
574
-
575
-
576
-
577
- #-----------------------------------------------------------------------
578
-
579
-
580
-
581
- def readLine():
582
-
583
- """
584
-
585
- Read and return as a string the next line of standard input.
586
-
587
- Raise an EOFError is there is no next line.
588
-
589
- """
590
-
591
- global _buffer
592
-
593
- if not hasNextLine():
594
-
595
- raise EOFError()
596
-
597
- s = _buffer
598
-
599
- _buffer = ''
600
-
601
- return s.rstrip('\n')
602
-
603
-
604
-
605
- #-----------------------------------------------------------------------
606
-
607
-
608
-
609
- def readAllLines():
610
-
611
- """
612
-
613
- Read all remaining lines from standard input, and return them as
614
-
615
- strings in an array.
616
-
617
- """
618
-
619
- lines = []
620
-
621
- while hasNextLine():
622
-
623
- line = readLine()
624
-
625
- lines.append(line)
626
-
627
- return lines
625
+ #=======================================================================
626
+
627
+ # For Testing
628
+
629
+ #=======================================================================
630
+
631
+
632
+
633
+ def _testWrite():
634
+
635
+ writeln()
636
+
637
+ writeln('string')
638
+
639
+ writeln(123456)
640
+
641
+ writeln(123.456)
642
+
643
+ writeln(True)
644
+
645
+ write()
646
+
647
+ write('string')
648
+
649
+ write(123456)
650
+
651
+ write(123.456)
652
+
653
+ write(True)
654
+
655
+ writeln()
656
+
657
+ writef('<%s> <%8d> <%14.8f>\n', 'string', 123456, 123.456)
658
+
659
+ writef('formatstring\n')
628
660
 
629
661
 
630
662
 
@@ -636,50 +668,4 @@
636
668
 
637
669
 
638
670
 
639
- #=======================================================================
640
-
641
- # For Testing
642
-
643
- #=======================================================================
644
-
645
-
646
-
647
- def _testWrite():
648
-
649
- writeln()
650
-
651
- writeln('string')
652
-
653
- writeln(123456)
654
-
655
- writeln(123.456)
656
-
657
- writeln(True)
658
-
659
- write()
660
-
661
- write('string')
662
-
663
- write(123456)
664
-
665
- write(123.456)
666
-
667
- write(True)
668
-
669
- writeln()
670
-
671
- writef('<%s> <%8d> <%14.8f>\n', 'string', 123456, 123.456)
672
-
673
- writef('formatstring\n')
674
-
675
-
676
-
677
- #-----------------------------------------------------------------------
678
-
679
-
680
-
681
- 省略
682
-
683
-
684
-
685
671
  ```