###課題
下記の課題を出題されました。
機能としては実装できましたが、よりよいアルゴリズム、実装方法があるのでは?と考えております。
アドバイスを頂きたく投稿致しました。
下記の2つのプログラムを作成致しました。
1.郵便番号のCSV (http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip)を使用し、住所レコードファイルを作成する機能
2.与えられた文字列中の文字をすべて含む住所レコードを上記で作成したインデックスを用いて検索し出力する機能。ただしスペースは文字として扱わない。
(データベースまた検索エンジンを使うのはNG)
###該当のソースコード
make_index_file_convert.rb
# initializae_variable I = 0 raw_str="" # call_packages require 'csv' require 'kconv' # read_csv_file read_file = ARGV[0] # make_index_file_and_write_record File.open("index_file.csv",'w') do |file| CSV.foreach('../data/KEN_ALL.CSV', encoding: "SJIS:UTF-8" ) do |row| output_record = "#{row[2]},#{row[6]},#{row[7]},#{row[8]}\n" puts output_record file.write(output_record) end end
# call_packages require 'csv' require 'kconv' # set_search_word search_word = ARGV[0] puts search_word class Array def which_elements_is_include(value) self.map.with_index{|item, i| i if item.include?(value)}.compact end end #CSV.open("index_file.csv","r") do |csv| CSV.foreach("index_file.csv") do |row| check = row.which_elements_is_include("渋谷") #現在は文言をベタがにしております。 array_length = check.length # p array_length if array_length != 0 p row end end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。