Advertisement
PonaFly

Untitled

Apr 19th, 2019
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.87 KB | None | 0 0
  1. (define (rising? n)
  2.  (define (iter n k)
  3.  (if(zero? n)
  4.  #t
  5.  (if(< k (remainder n 10))
  6.  #f
  7.  (iter (quotient n 10) (remainder n 10)))))
  8.  (iter (quotient n 10) (remainder n 10)))
  9. (define (sum1 file)
  10.  (define in (open-input-file file))
  11.  (define (next num sum lst)
  12.  (define n (read-char in))
  13.  (cond [(eq? n eof) (begin (close-input-port in) (if(rising? (+ num sum)) (write-to-file (sort (cons (+ num sum) lst) >)) (write-to-file (sort lst >))))]
  14.  [(eq? n #\+) (next 0 num lst)]
  15.  [(eq? n #\return) (begin (read-char in) (next 0 0 (if (rising? (+ num sum)) (cons (+ num sum) lst) lst)))]
  16.  [else (next (+ (* 10 num) (- (char->integer n) 48)) sum lst)]))
  17.  (next 0 0 null))
  18. (define (write-to-file lst)
  19.  (define out (open-output-file "output.txt" #:exists 'replace))
  20.  (define (iter lst)
  21.  (if(null? lst)
  22.  (close-output-port out)
  23.  (begin (display (car lst) out) (display "\r\n" out) (iter (cdr lst)))))
  24.  (iter lst))
  25.  
  26.  
  27.  
  28.  
  29.  
  30. #lang scheme
  31.  (define (read-graph filename)
  32.  (define (read-vertex)
  33.  (define n (read in))
  34.  (foldl (lambda (i lst)
  35.  (if (= i n) lst
  36.  (cons (read in) lst)))
  37.  '()
  38.  (build-list n values)))
  39.  (define in (open-input-file filename))
  40.  (define n (read in))
  41.  (reverse
  42.  (foldl (lambda (i lst)
  43.  (if (= i n) lst
  44.  (cons (read-vertex) lst)))
  45.  '()
  46.  (build-list n values))))
  47.  
  48. ;4
  49. ; 3 1 2 3 - 0 (1 2 3) (0 2 3) (0 1 3) (0 1 2)
  50. ; 3 0 2 3 1
  51. ; 3 0 1 3 2
  52. ; 3 0 1 2 3
  53. ;в каждом проверить member ДО k-1
  54. ;2
  55.  
  56. (define (Sol2 filename)
  57.  
  58.  
  59.  (define k (read (open-input-file filename)))
  60.  (define filelst (read-graph filename))
  61.  (define (searching lest)
  62.  (if (empty? lest) #t
  63.  (if (= (length (car lest)) (- k 1)) (searching (cdr lest)) #f)))
  64.  (searching filelst))
  65.  
  66.  
  67. (define (my-cdar lst)
  68.  (if (= (length lst) 1) (cdr lst)
  69.  (cons (cdar lst) (cdr lst))))
  70.  
  71. (define (Sol3 filename)
  72.  (define out (open-output-file "output.txt" #:exists 'replace))
  73.  (define filelst (read-graph filename))
  74.  (define r (read (open-input-file filename)))
  75.  (define (creating k lest rez)
  76.  (if (empty? lest) rez
  77.  (if (empty? (car lest)) (creating (add1 k) (cdr lest) rez)
  78.  (if (= (length (car lest)) 1) (creating (add1 k) (cdr lest) (cons (cons k (caar lest)) rez))
  79.  (creating k (my-cdar lest) (cons (cons k (caar lest)) rez)) ))))
  80.  
  81.  
  82.  (define result (creating 0 filelst (cons (cons (- (length filelst) 1) (car (reverse (car (reverse filelst))))) '())))
  83.  (define (outting file rez)
  84.  (if (empty? rez) (close-output-port file)
  85.  (begin (display (car rez) file)
  86. (write-char #\newline file )
  87. (write-char #\return file)
  88. (outting file (cdr rez))
  89.  )))
  90.  (outting out result))
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. (define (Sol1 filename)
  98.  (define filelst (read-graph filename))
  99.  (define (searching k lst)
  100.  (if (empty? lst) k
  101.  (if (empty? (car lst)) (searching (add1 k) (cdr lst))
  102.  (searching k (cdr lst)))))
  103.  (searching 0 filelst))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement