Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me translate this racket code to python #lang racket ;;; Simple

ID: 3777738 • Letter: C

Question

can someone help me translate this racket code to python

#lang racket

;;; Simple matrix representation

(struct matrix (rows-n cols-n (data-vector #:mutable)))

(define (make-matrix rows-n cols-n)
(define size (* rows-n cols-n))
(matrix rows-n cols-n (make-vector size 0)))

(define (matrix-set! m i j v)
(define data-vector (matrix-data-vector m))
(define cols-n (matrix-cols-n m))
(define index (+ (* i cols-n) j))
(vector-set! data-vector index v))

(define (matrix-ref m i j)
(define data-vector (matrix-data-vector m))
(define cols-n (matrix-cols-n m))
(define index (+ (* i cols-n) j))
(vector-ref data-vector index))

;;; (read-marvel-network filename filename) -> (vector-of string?)
;;;                                            (vector-of string?)
;;;                                            matrix?
;;;   filename : string?
;;; Reads the Marvel social network from the specified filename. Returns a vector
;;; of the character names, a vector of the book names, and the appearance
;;; matrix.
(define (read-marvel-network filename)
(define port (open-input-file filename #:mode 'text))
;; Read *Vertices line
(unless (eq? (read port) '*Vertices)
    (error 'read-marvel-network "Expected *Vertices line"))
(define n (read port))
(define characters-n (read port))
(define books-n (- n characters-n))
;; Read character names
(define character-vector (make-vector characters-n))
(for ((i (in-range characters-n)))
    (define index (read port))
    (define name (read port))
    (vector-set! character-vector (- index 1) name))
;; Read book names
(define book-vector (make-vector books-n))
(for ((i (in-range books-n)))
    (define index (read port))
    (define name (read port))
    (vector-set! book-vector (- index characters-n 1) name))
;; Read *Edgeslist line
(unless (eq? (read port) '*Edgeslist)
    (error 'read-marvel-network "Expected *Edgeslist line"))
(define appearance-matrix (make-matrix characters-n books-n))
(let loop ((buffer (read-line port)))
    (unless (eof-object? buffer)
      (define parsed-buffer (string-split buffer))
      (unless (null? parsed-buffer)
        (define i (- (string->number (car parsed-buffer)) 1))
        (for ((index (in-list (cdr parsed-buffer))))
          (define j (- (string->number index) characters-n 1))
          (matrix-set! appearance-matrix i j 1)))
      (loop (read-line port))))
;; Close the file and return.
(close-input-port port)
(values character-vector book-vector appearance-matrix))

;;; (spidey-number collaborations i) -> (or/c exact-nonnegative-integer? +inf.0)
;;;   collaborations : matrix?
;;;   i : exact-nonnegative-integer?
;;; Returns the Spider Man number for character i.
(define (spidey-number collaborations i)
(define spidey 5305) ; The character number for Spider Man.
(define characters-n (matrix-rows-n collaborations))
(define connections (make-vector characters-n 0))
(vector-set! connections i 1)
(let/ec exit
    (for ((n (in-range 7)))
      (when (> (vector-ref connections spidey) 0)
        (exit n))
      (define new-connections (make-vector characters-n 0))
      (for ((j (in-range characters-n)))
        (when (> (vector-ref connections j) 0)
          (for ((k (in-range characters-n)))
            (vector-set!
             new-connections k
             (+ (vector-ref new-connections k)
                (matrix-ref collaborations j k))))))
      (set! connections new-connections))
    +inf.0))

;;; (write-dot-file names collaborations) -> void?
;;;   names : (vector-of string?)
;;;   collaborations : matrix?
;;; Writes a Graphviz dot file for Spider Man's immediate collaborations.
(define (write-dot-file names collaborations)
(define spidey 5305) ; The character number for Spider Man.
(define rows-n (matrix-rows-n collaborations))
(define cols-n (matrix-cols-n collaborations))
(define port (open-output-file "marvel.dot" #:mode 'text #:exists 'replace))
(fprintf port "graph G {~n")
(for ((name (in-vector names))
        (i (in-naturals)))
    (when (> (matrix-ref collaborations i spidey) 0)
      (fprintf port " n~a [label = ~s] ;~n" i name)))
(for* ((i (in-range rows-n))
         (j (in-range 0 i)))
    (define w (matrix-ref collaborations i j))
    (when (and (> w 0) (or (= i spidey) (= j spidey)))
      (fprintf port "n~a -- n~a [label = "~a"] ;~n" i j w)))
(fprintf port "}~n")
(close-output-port port))

;;; (main) -> void?
(define (main)
; Read the Marvel social network.
(printf "Reading Marvel universe network.~n")
(define-values (characters books appearances)
    (read-marvel-network "porgat.txt"))
(define characters-n (vector-length characters))
(define books-n (vector-length books))
(printf "Number of characters     = ~a~n" characters-n)
(printf "Number of books          = ~a~n" books-n)
;; Compute total appearances
(printf "Computing total appearances.~n")
(define total-appearances
    (for*/sum ((i (in-range characters-n))
               (j (in-range books-n)))
      (matrix-ref appearances i j)))
(printf "Mean books per character = ~a~n"
          (~r (/ total-appearances characters-n) #:precision 2))
(printf "Mean characters per book = ~a~n"
          (~r (/ total-appearances books-n) #:precision 2))
;; Compute books per character histogram.
(printf "Computing books per character histogram.~n")
(let ((h (make-discrete-histogram)))
    (for ((i (in-range characters-n)))
      (discrete-histogram-increment!
       h
       (for/sum ((j (in-range books-n)))
         (matrix-ref appearances i j))))
    (printf "~s~n" (discrete-histogram-plot h "Books per Character Histogram")))
;; Compute characters per book histogram.
(printf "Computing characters per book histogram.~n")
(let ((h (make-discrete-histogram)))
    (for ((j (in-range books-n)))
      (discrete-histogram-increment!
       h
       (for/sum ((i (in-range characters-n)))
         (matrix-ref appearances i j))))
    (printf "~s~n" (discrete-histogram-plot h "Characters per Book Histogram")))
;; Compute collaboration matrix
(printf "Computing collaboration matrix.~n")
(define collaborations (make-matrix characters-n characters-n))
(for* ((i (in-range characters-n))
         (j (in-range books-n)))
    (when (> (matrix-ref appearances i j) 0)
      (for ((k (in-range characters-n)))
        (when (> (matrix-ref appearances k j) 0)
          (matrix-set! collaborations i k
                       (+ (matrix-ref collaborations i k) 1))))))
;; Print a small portion of it.
(for ((i (in-range 30)))
    (for ((j (in-range 30)))
      (printf "~a" (~r (matrix-ref collaborations i j)
                        #:min-width 3)))
    (printf " ...~n"))
(printf "...~n")
;; Compute collaborations histogram.
(printf "Computing collaboration histogram.~n")
(let ((h (make-discrete-histogram)))
    (for* ((i (in-range characters-n))
           (j (in-range i)))
      (when (> (matrix-ref collaborations i j) 0)
        (discrete-histogram-increment!
         h (matrix-ref collaborations i j))))
    (printf "~s~n" (discrete-histogram-plot h "Collaborations Histogram")))
;; Define a function to print Spider Man numbers.
(define (print-spidey-number c)
    (define sn (spidey-number collaborations c))
    (if (= sn +inf.0)
        (printf "~a has a Spider Man number greater than 6.~n"
                (vector-ref characters c))
        (printf "~a has a Spider Man number of ~a.~n"
                (vector-ref characters c) sn)))
;; Compute Spider Man number histogram
(printf "Computing Spider Man number histogram.~n")
(let ((h (make-discrete-histogram))
        (n 0))
    (for ((i (in-range characters-n)))
      (define sn (spidey-number collaborations i))
      (if (= sn +inf.0)
          (set! n (+ n 1))
          (discrete-histogram-increment! h sn)))
    (printf "There are ~a characters with no Spider Man number.~n" n)
    (printf "~s~n" (discrete-histogram-plot h "Spider Man Number Histogram")))
;; Print Spider Man numbers for random characters.
(printf "Printing Spider Man numbers for random characters.~n")
(for ((i (in-range 100)))
    (print-spidey-number (random characters-n)))
;; Print Spider Man numbers for all caracters.
; (printf "Printing Spider Man numbers for all characters.~n")
; (for ((i (in-range characters-n)))
;    (print-spidey-number i))
;; Write Graphviz dot file.
(printf "Writing Graphviz dot file.~n")
(write-dot-file characters collaborations))

(time (main))

Explanation / Answer

To implement a BST that stores some data with each key, we would use the following class definitions (changes are in red):

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote