Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lukestringer90/fd77cfcb39bbf190fa9ebc5f7e6ce324 to your computer and use it in GitHub Desktop.
Save lukestringer90/fd77cfcb39bbf190fa9ebc5f7e6ce324 to your computer and use it in GitHub Desktop.
Solution to Code Wars 5547cc7dcad755e480000004
// Problem statement: https://www.codewars.com/kata/5547cc7dcad755e480000004/train/swift
import Foundation
func removNb(_ length: Int) -> [(Int,Int)] {
var matches = [(Int,Int)]()
var seenNumbers = [Int]()
let sumOfNumbers = ((length * length) + length) / 2
var numbersToTry = Array(Array(1...length)[1...length-1].reversed()).makeIterator()
while (true) {
guard let attempt = numbersToTry.next() else { break }
// Don't try numbers already seen
guard !seenNumbers.contains(attempt) else {
print("Already seen \(attempt), skipping ↩️")
continue
}
// Solve the equation
let computedFloat = Float(sumOfNumbers - attempt) / Float(attempt + 1)
// If larger the count then all others will be too large, so totally stop
guard computedFloat < Float(length) else {
print("\(computedFloat) too large, Stopping at \(attempt) ❌")
break
}
let computedInt = Int(computedFloat)
// If computed is a whole number and less than the length it is a match
if computedInt < length {
if floor(computedFloat) == computedFloat {
matches.append((attempt, computedInt))
matches.append((computedInt, attempt))
// Skip this computed number if we see it next time as an attemp
seenNumbers.append(computedInt)
print("\(attempt) => \(computedFloat) ✅")
}
else {
print("\(attempt) => \(computedFloat)")
}
}
else {
print("\(attempt) => \(computedFloat)")
}
// Skip over these next time
seenNumbers.append(attempt)
}
// Put tuple values in the right order
return matches.map { pair in
return (pair.1, pair.0)
}
}
print(removNb(906))
@lukestringer90
Copy link
Author

Running with 906 gives the following:

906 => 452.0011
905 => 452.5011
904 => 453.0022
903 => 453.50443
902 => 454.00775
901 => 454.5122
900 => 455.01776
899 => 455.52444
898 => 456.03226
897 => 456.5412
896 => 457.05127
895 => 457.5625
894 => 458.07486
893 => 458.58838
892 => 459.10303
891 => 459.61884
890 => 460.1358
889 => 460.65393
888 => 461.17322
887 => 461.6937
886 => 462.21533
885 => 462.73816
884 => 463.26215
883 => 463.78732
882 => 464.3137
881 => 464.84128
880 => 465.37003
879 => 465.9
878 => 466.43118
877 => 466.96356
876 => 467.49716
875 => 468.03195
874 => 468.568
873 => 469.10526
872 => 469.64377
871 => 470.18347
870 => 470.72446
869 => 471.26666
868 => 471.81012
867 => 472.35483
866 => 472.90082
865 => 473.44803
864 => 473.99652
863 => 474.5463
862 => 475.09732
861 => 475.64966
860 => 476.20325
859 => 476.75815
858 => 477.31433
857 => 477.8718
856 => 478.43057
855 => 478.99066
854 => 479.55203
853 => 480.11475
852 => 480.67877
851 => 481.24414
850 => 481.81082
849 => 482.3788
848 => 482.94818
847 => 483.51886
846 => 484.0909
845 => 484.6643
844 => 485.23904
843 => 485.81516
842 => 486.39264
841 => 486.9715
840 => 487.55173
839 => 488.13333
838 => 488.71634
837 => 489.30072
836 => 489.8865
835 => 490.4737
834 => 491.0623
833 => 491.65228
832 => 492.24368
831 => 492.83655
830 => 493.43082
829 => 494.02652
828 => 494.62366
827 => 495.22223
826 => 495.82224
825 => 496.42374
824 => 497.02667
823 => 497.63107
822 => 498.23694
821 => 498.84427
820 => 499.4531
819 => 500.06342
818 => 500.6752
817 => 501.2885
816 => 501.9033
815 => 502.51962
814 => 503.13742
813 => 503.75674
812 => 504.37762
811 => 505.0 ✅
810 => 505.62393
809 => 506.2494
808 => 506.8764
807 => 507.50494
806 => 508.13507
805 => 508.76675
804 => 509.4
803 => 510.03482
802 => 510.67123
801 => 511.30923
800 => 511.94882
799 => 512.59
798 => 513.2328
797 => 513.8772
796 => 514.5232
795 => 515.17084
794 => 515.8201
793 => 516.471
792 => 517.1236
791 => 517.7778
790 => 518.43365
789 => 519.0911
788 => 519.7503
787 => 520.4112
786 => 521.07367
785 => 521.7379
784 => 522.4038
783 => 523.0714
782 => 523.7407
781 => 524.41174
780 => 525.08453
779 => 525.759
778 => 526.4352
777 => 527.1131
776 => 527.7928
775 => 528.47424
774 => 529.1574
773 => 529.8424
772 => 530.5291
771 => 531.2176
770 => 531.9079
769 => 532.6
768 => 533.2939
767 => 533.98956
766 => 534.6871
765 => 535.3864
764 => 536.0876
763 => 536.7906
762 => 537.4954
761 => 538.2021
760 => 538.91064
759 => 539.62103
758 => 540.3333
757 => 541.0475
756 => 541.76355
755 => 542.4815
754 => 543.20135
753 => 543.9231
752 => 544.6467
751 => 545.3723
750 => 546.09985
749 => 546.82935
748 => 547.5607
747 => 548.2941
746 => 549.0295
745 => 549.7668
744 => 550.50604
743 => 551.2473
742 => 551.9906
741 => 552.73584
740 => 553.48315
739 => 554.2324
738 => 554.98376
737 => 555.7371
736 => 556.49255
735 => 557.25
734 => 558.0095
733 => 558.7711
732 => 559.5348
731 => 560.30054
730 => 561.0684
729 => 561.8384
728 => 562.6104
727 => 563.38464
726 => 564.16095
725 => 564.9394
724 => 565.72
723 => 566.50275
722 => 567.2877
721 => 568.07477
720 => 568.8641
719 => 569.6556
718 => 570.4492
717 => 571.2451
716 => 572.0432
715 => 572.84357
714 => 573.6462
713 => 574.451
712 => 575.25806
711 => 576.06744
710 => 576.879
709 => 577.69293
708 => 578.50916
707 => 579.3277
706 => 580.1485
705 => 580.9717
704 => 581.7972
703 => 582.625
702 => 583.4552
701 => 584.2877
700 => 585.1227
699 => 585.96
698 => 586.79974
697 => 587.64185
696 => 588.4864
695 => 589.3333
694 => 590.18274
693 => 591.0346
692 => 591.8889
691 => 592.74567
690 => 593.6049
689 => 594.4667
688 => 595.33093
687 => 596.1977
686 => 597.06696
685 => 597.9388
684 => 598.8131
683 => 599.69006
682 => 600.5695
681 => 601.4516
680 => 602.33624
679 => 603.2235
678 => 604.1134
677 => 605.0059
676 => 605.90106
675 => 606.7988
674 => 607.6993
673 => 608.60236
672 => 609.5082
671 => 610.4167
670 => 611.3279
669 => 612.2418
668 => 613.15845
667 => 614.0778
666 => 615.0 ✅
665 => 615.9249
664 => 616.85266
663 => 617.78314
662 => 618.71643
661 => 619.6526
660 => 620.59155
659 => 621.5333
658 => 622.478
657 => 623.42554
656 => 624.376
655 => 625.3293
654 => 626.2855
653 => 627.2446
652 => 628.2067
651 => 629.17175
650 => 630.1398
649 => 631.1108
648 => 632.0847
647 => 633.0617
646 => 634.04175
645 => 635.0248
644 => 636.01086
643 => 637.0 ✅
642 => 637.99225
641 => 638.98755
640 => 639.98596
639 => 640.9875
638 => 641.9922
Already seen 637, skipping ↩️
636 => 644.011
635 => 645.02515
634 => 646.04254
633 => 647.0631
632 => 648.0869
631 => 649.11395
630 => 650.1442
629 => 651.1778
628 => 652.2146
627 => 653.25476
626 => 654.2982
625 => 655.34503
624 => 656.3952
623 => 657.4487
622 => 658.5056
621 => 659.5659
620 => 660.62964
619 => 661.6968
618 => 662.7674
617 => 663.84143
616 => 664.91895
Already seen 615, skipping ↩️
614 => 667.08453
613 => 668.17267
612 => 669.2643
611 => 670.3595
610 => 671.45825
609 => 672.56067
608 => 673.6667
607 => 674.7763
606 => 675.88965
605 => 677.0066
604 => 678.12726
603 => 679.25165
602 => 680.37976
601 => 681.51166
600 => 682.6473
599 => 683.7867
598 => 684.9299
597 => 686.0769
596 => 687.2278
595 => 688.38257
594 => 689.5412
593 => 690.7037
592 => 691.8702
591 => 693.0405
590 => 694.2149
589 => 695.39325
588 => 696.57556
587 => 697.7619
586 => 698.9523
585 => 700.1467
584 => 701.3453
583 => 702.548
582 => 703.7547
581 => 704.96564
580 => 706.1807
579 => 707.4
578 => 708.6235
577 => 709.8512
576 => 711.0832
575 => 712.31946
574 => 713.56
573 => 714.8049
572 => 716.0541
571 => 717.3077
570 => 718.5657
569 => 719.82806
568 => 721.0949
567 => 722.3662
566 => 723.64197
565 => 724.92224
564 => 726.2071
563 => 727.49646
562 => 728.7904
561 => 730.089
560 => 731.39215
559 => 732.7
558 => 734.0125
557 => 735.3298
556 => 736.65173
555 => 737.9784
554 => 739.30994
553 => 740.6462
552 => 741.98737
551 => 743.3333
550 => 744.6842
549 => 746.04
548 => 747.40076
547 => 748.7664
546 => 750.1371
545 => 751.5128
544 => 752.89355
543 => 754.2794
542 => 755.67035
541 => 757.0664
540 => 758.46765
539 => 759.8741
538 => 761.2857
537 => 762.7026
536 => 764.12476
535 => 765.55225
534 => 766.98505
533 => 768.4232
532 => 769.8668
531 => 771.3158
530 => 772.77026
529 => 774.23016
528 => 775.6957
527 => 777.1667
526 => 778.64325
525 => 780.1255
524 => 781.61334
523 => 783.1069
522 => 784.60614
521 => 786.1111
520 => 787.6219
519 => 789.1385
518 => 790.6609
517 => 792.1892
516 => 793.7234
515 => 795.26355
514 => 796.8097
513 => 798.3619
512 => 799.9201
511 => 801.4844
510 => 803.0548
509 => 804.63135
508 => 806.2142
507 => 807.80316
506 => 809.39844
Already seen 505, skipping ↩️
504 => 812.6079
503 => 814.2222
502 => 815.84296
501 => 817.4701
500 => 819.1038
499 => 820.744
498 => 822.3908
497 => 824.0442
496 => 825.7042
495 => 827.371
494 => 829.04443
493 => 830.7247
492 => 832.41174
491 => 834.1057
490 => 835.8065
489 => 837.5143
488 => 839.22906
487 => 840.9508
486 => 842.6797
485 => 844.41565
484 => 846.15875
483 => 847.9091
482 => 849.6667
481 => 851.4315
480 => 853.20374
479 => 854.98334
478 => 856.7703
477 => 858.5649
476 => 860.3669
475 => 862.17645
474 => 863.9937
473 => 865.81854
472 => 867.6512
471 => 869.4915
470 => 871.3397
469 => 873.19574
468 => 875.0597
467 => 876.93164
466 => 878.8116
465 => 880.6996
464 => 882.5957
463 => 884.5
462 => 886.41254
461 => 888.3333
460 => 890.26245
459 => 892.2
458 => 894.146
457 => 896.10046
456 => 898.0635
455 => 900.0351
454 => 902.0154
453 => 904.0044
906.0022 too large, Stopping at 452 ❌
[(505, 811), (811, 505), (615, 666), (666, 615), (637, 643), (643, 637)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment