【问题标题】:How to populate an array from size (equal to user input) with random unique numbers? CLOJURE如何使用随机唯一数字填充大小(等于用户输入)的数组? CLOJURE
【发布时间】:2017-02-01 14:03:25
【问题描述】:

目前正在编程冒泡排序算法。 我找到了网上某人的源代码:

Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array which you would like to create: ");

应用程序现在做什么,它接受用户的输入数字。并制作一个随机数数组。使用这个数组,它应用了冒泡排序算法。

现在,我正在尝试将其翻译为 Clojure。 我知道用户输入可以通过函数来​​实现:

Flush

read-line

除此之外,我不明白如何获取所需数组大小的用户输入,然后创建一个随机数数组,然后应用冒泡排序。 到目前为止,这就是我所拥有的(从 Rosetta 网站找到的 Bubblesort。):

 (ns BubbleSort
  (:import java.util.ArrayList)
  (:import (java.util Date)))

(defn safe-println [& more]
  (.write *out* (str (clojure.string/join " " more) "\n")))


; set a timestamp
(defn restart-profiling []
  (def last-time-stamp (atom (System/nanoTime))
    )
  )

; get ms since last timestamp
(defn get-delta-ms []
  (let [last @last-time-stamp
        current (System/nanoTime)
        ticks (- current last)
        ]

    (restart-profiling)
    (float (/ ticks 1000000)) ; calculate the delta in milliseconds
    )
  )


(defn bubble-sort
  "Sort in-place.
  arr must implement the Java List interface and should support
  random access, e.g. an ArrayList."
  ([arr] (bubble-sort compare arr))
  ([cmp arr]
   (letfn [(swap! [i j]
             (let [t (.get arr i)]
               (doto arr
                 (.set i (.get arr j))
                 (.set j t))))
           (sorter [stop-i]
             (let [changed (atom false)]
               (doseq [i (range stop-i)]
                 (if (pos? (cmp (.get arr i) (.get arr (inc i))))
                   (do
                     (swap! i (inc i))
                     (reset! changed true))))
               @changed))]
     (doseq [stop-i (range (dec (.size arr)) -1 -1)
             :while (sorter stop-i)])
     arr)))

(restart-profiling)
(println (bubble-sort (ArrayList. [10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6 5 4 3 2 1
                                   10 9 8 7 6


                                   ])))

(safe-println "The serial implementation "(get-delta-ms) "ms")

需要:

  • 用户输入
  • 创建用户输入大小的随机数数组

提前致谢!

编辑:

用户输入:

    def(input
      (println "Enter the size of the array: ")
    )
    println(read-line)

输入应确定数组的大小。

剩余部分:

用输入表示的大小的唯一随机数填充数组。

【问题讨论】:

    标签: java algorithm intellij-idea clojure bubble-sort


    【解决方案1】:

    read-line读取输入后,可以用read-string解析成整数,然后用rand生成随机数。之后,使用to-array 将它们转换为Java 数组。以下函数将提示用户输入数组大小并返回一个从 0 到 1 的随机数数组:

    (defn random-array-prompt []
      (print "Enter array size:")
      (let [n (read-string (read-line))]
        (to-array (repeatedly n rand))))
    

    之后,就可以在repl中调用那个函数了:

    (println (seq (bubble-sort (random-array-prompt))))
    

    这将提示您输入尺寸并打印排序后的序列。

    【讨论】:

    • 感谢您的评论。我对clojure很陌生,也许我理解不正确,但这不起作用?有什么建议吗?
    • 能否请您更具体一些 - 您是否收到任何错误消息等?
    • 线程 "main" java.lang.RuntimeException 中的异常:读取时出现 EOF,这就是我收到的错误消息..
    • 我的错,我的 sn-p 中缺少一个括号(和 def 而不是 defn)。更正并添加了一些解释 - 请查看答案。
    • 另外,如果你在学习 Clojure 的同时有 Java 背景,那么从一般性的 Clojure 简介开始,像这样:braveclojure.com/clojure-for-the-brave-and-true,然后学习如何使用 Java 数组和与 Java 集成。
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多