From 37953892d1b7d1f65faa103ebe6d4faa8b8dd83f Mon Sep 17 00:00:00 2001 From: ypsl <43265168+ypsl@users.noreply.github.com> Date: Sat, 8 Dec 2018 22:20:44 +0300 Subject: [PATCH 01/22] Add files via upload --- Hryvicki Kirill/0/loadFiles.rb | 18 +++ Hryvicki Kirill/0/run.rb | 262 +++++++++++++++++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 Hryvicki Kirill/0/loadFiles.rb create mode 100644 Hryvicki Kirill/0/run.rb diff --git a/Hryvicki Kirill/0/loadFiles.rb b/Hryvicki Kirill/0/loadFiles.rb new file mode 100644 index 0000000..2691542 --- /dev/null +++ b/Hryvicki Kirill/0/loadFiles.rb @@ -0,0 +1,18 @@ +require 'mechanize' +require 'open-uri' +require 'uri' + +agent = Mechanize.new +page = agent.get('http://www.belstat.gov.by/ofitsialnaya-statistika/makroekonomika-i-okruzhayushchaya-sreda/tseny/operativnaya-informatsiya_4/srednie-tseny-na-potrebitelskie-tovary-i-uslugi-po-respublike-belarus') +page.links_with(:href => /.xls/).each do |link| + str_link = link.href.to_s + str_link = URI.unescape(str_link) + str_link = URI.escape(str_link) + if /http:\/\/www.belstat.gov.by/ === str_link + f_link = str_link + else + f_link = 'http://www.belstat.gov.by' + str_link + end + download = open(f_link) + IO.copy_stream(download, "./data/#{download.base_uri.to_s.split('/')[-1]}") +end \ No newline at end of file diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb new file mode 100644 index 0000000..a9f67eb --- /dev/null +++ b/Hryvicki Kirill/0/run.rb @@ -0,0 +1,262 @@ +require 'roo' +require 'roo-xls' + +def getFileInstance(filePath) + ext = filePath.split(".")[2] + fileInstance = nil + + if ext == "xls" || ext == "xlsx" + fileInstance = Roo::Spreadsheet.open(filePath) + case ext + when "xls" + fileInstance = Roo::Excel.new(filePath) + when "xlsx" + fileInstance = Roo::Excelx.new(filePath) + end + else + puts "unsupported file type: " + filePath + end + + return fileInstance +end + +def findKeys(word, products) + result = [] + + products.keys.each{ |key| + if(key.include?(word)) + result.push(key) + end + } + return result +end + +def fetchProductsData(fileInstance, products, regions) + + for n in 9..fileInstance.last_row + if fileInstance.cell("E", n) == nil + next + end + + year = fileInstance.cell("A", 3).split(" ")[2] + month = fileInstance.cell("A", 3).split(" ")[1] + + key = fileInstance.cell("A", n).strip.downcase + + if !products[key] + products[key] = Hash.new + end + if !products[key][year] + products[key][year] = Hash.new + end + + products[key][year][month] = { + regions[0] => formatValue(fileInstance.cell("G", n), year), + regions[1] => formatValue(fileInstance.cell("I", n), year), + regions[2] => formatValue(fileInstance.cell("K", n), year), + regions[3] => formatValue(fileInstance.cell("M", n), year), + regions[4] => formatValue(fileInstance.cell("O", n), year), + regions[5] => formatValue(fileInstance.cell("Q", n), year), + regions[6] => formatValue(fileInstance.cell("S", n), year) + } + end +end + +def formatValue(val, year) + if val + result = val.to_f + + if year.to_i < 2017 + result = result / 10000 + end + + return result.round(2) + end +end + +def getRecentPriceData(key, products, monthMap) + currentYear = Time.now.strftime("%Y").to_s + + currentMonth = Time.now.strftime("%m") + + monthMap.each{|month, monthNumber| + if monthNumber == currentMonth + currentMonth == month + end + } + + productYearData = products[key]; + + if productYearData[currentYear] + yearKey = currentYear + else + begin + yearKey = productYearData.keys.max{|a,b| a.to_i <=> b.to_i} + rescue + puts productYearData.keys + end + + end + + productMonthData = productYearData[yearKey] + + if productMonthData[currentMonth] + monthKey = currentMonth + else + # begin + # monthKey = productMonthData.keys.max{|a,b| monthMap[a].to_i <=> monthMap[b].to_i} + # rescue + # puts productMonthData.keys + + # dr = { + # "апрель" => "dsad", + # "май" => "dasd" + # } + # dr.keys.max{|a,b| monthMap[a] <=> monthMap[b]} + # end + monthKey = productMonthData.keys.max{|a,b| monthMap[a].to_i <=> monthMap[b].to_i} + end + + return { + "price" => productMonthData[monthKey]["Minsk"], + "year" => yearKey, + "month" => monthKey, + "product" => key + } +end + +def getMinPrice(hash) + result = Hash.new + minYearPrice = 9999999999999 + + hash.each{|year, yearHash| + minMonthPrice = 9999999999999 + yearHash.each{|month, monthHash| + price = monthHash['Minsk'] + if !price + next + end + if(price < minMonthPrice) + minMonthPrice = price + result['month'] = month + end + } + if(minMonthPrice < minYearPrice) + minYearPrice = minMonthPrice + result['year'] = year + result['price'] = minYearPrice + end + } + return result +end + +def getMaxPrice(hash) + result = Hash.new + maxYearPrice = 0 + + hash.each{|year, yearHash| + maxMonthPrice = 0 + yearHash.each{|month, monthHash| + price = monthHash['Minsk'] + if !price + next + end + if(price > maxMonthPrice) + maxMonthPrice = price + result['month'] = month + end + } + if(maxMonthPrice > maxYearPrice) + maxYearPrice = maxMonthPrice + result['year'] = year + result['price'] = maxYearPrice + end + } + return result +end + +def getSimilarPriceProducts(data, products) + result = [] + price = data["price"] + year = data["year"] + month = data["month"] + originProduct = data["product"] + + products.each{|product, productData| + if !productData[year] + next + end + if !productData[year][month] + next + end + if productData[year][month]["Minsk"] == price && product != originProduct + result.push(product) + end + } + return result +end + +def main + monthMap = { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12, + } + + regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + products = Hash.new + filePaths = Dir["./data/*"] + + + filePaths.each{ |filePath| + fileInstance = getFileInstance(filePath) + + if fileInstance + fetchProductsData(fileInstance, products, regions) + end + } + + while true + + puts "What price are you looking for?" + + word = gets.chomp.downcase + + keys = findKeys(word, products) + + if keys.length == 0 + puts word.capitalize + " can not be found in database" + else + keys.each{|key| + puts "" + recentPriceData = getRecentPriceData(key, products, monthMap) + puts key.capitalize + " is " + recentPriceData["price"].to_s + " BYN in Minsk these days." + minPrice = getMinPrice(products[key]) + puts "Lowest was on " + minPrice["year"] + "/" + monthMap[minPrice["month"]].to_s + " at price " + minPrice["price"].to_s + " BYN" + maxPrice = getMaxPrice(products[key]) + puts "Maximum was on " + maxPrice["year"] + "/" + monthMap[maxPrice["month"]].to_s + " at price " + maxPrice["price"].to_s + " BYN" + + similarProducts = getSimilarPriceProducts(recentPriceData, products) + + if similarProducts.length == 0 + puts "No products for similar price" + else + puts "For similar price you also can afford" + puts similarProducts + end + + } + end + end +end + +main() \ No newline at end of file From c595793c71c6c0a3bafbcdb8c967f5ca4dd9a47e Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 8 Dec 2018 23:35:20 +0300 Subject: [PATCH 02/22] Update run.rb small fixes --- Hryvicki Kirill/0/run.rb | 431 +++++++++++++++++++-------------------- 1 file changed, 205 insertions(+), 226 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index a9f67eb..dce8e97 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -2,261 +2,240 @@ require 'roo-xls' def getFileInstance(filePath) - ext = filePath.split(".")[2] - fileInstance = nil - - if ext == "xls" || ext == "xlsx" - fileInstance = Roo::Spreadsheet.open(filePath) - case ext - when "xls" - fileInstance = Roo::Excel.new(filePath) - when "xlsx" - fileInstance = Roo::Excelx.new(filePath) - end - else - puts "unsupported file type: " + filePath - end - - return fileInstance + ext = filePath.split(".")[2] + fileInstance = nil + + if ext == "xls" || ext == "xlsx" + fileInstance = Roo::Spreadsheet.open(filePath) + case ext + when "xls" + fileInstance = Roo::Excel.new(filePath) + when "xlsx" + fileInstance = Roo::Excelx.new(filePath) + end + else + puts "unsupported file type: " + filePath + end + + return fileInstance end def findKeys(word, products) - result = [] - - products.keys.each{ |key| - if(key.include?(word)) - result.push(key) - end - } - return result + result = [] + + products.keys.each { |key| + if(key.include?(word)) + result.push(key) + end + } + return result end def fetchProductsData(fileInstance, products, regions) - for n in 9..fileInstance.last_row - if fileInstance.cell("E", n) == nil - next - end - - year = fileInstance.cell("A", 3).split(" ")[2] - month = fileInstance.cell("A", 3).split(" ")[1] - - key = fileInstance.cell("A", n).strip.downcase - - if !products[key] - products[key] = Hash.new - end - if !products[key][year] - products[key][year] = Hash.new - end - - products[key][year][month] = { - regions[0] => formatValue(fileInstance.cell("G", n), year), - regions[1] => formatValue(fileInstance.cell("I", n), year), - regions[2] => formatValue(fileInstance.cell("K", n), year), - regions[3] => formatValue(fileInstance.cell("M", n), year), - regions[4] => formatValue(fileInstance.cell("O", n), year), - regions[5] => formatValue(fileInstance.cell("Q", n), year), - regions[6] => formatValue(fileInstance.cell("S", n), year) - } - end + for n in 9..fileInstance.last_row + if fileInstance.cell("E", n) == nil + next + end + + year = fileInstance.cell("A", 3).split(" ")[2] + month = fileInstance.cell("A", 3).split(" ")[1] + + key = fileInstance.cell("A", n).strip.downcase + + if !products[key] + products[key] = Hash.new + end + if !products[key][year] + products[key][year] = Hash.new + end + + products[key][year][month] = { + regions[0] => formatValue(fileInstance.cell("G", n), year), + regions[1] => formatValue(fileInstance.cell("I", n), year), + regions[2] => formatValue(fileInstance.cell("K", n), year), + regions[3] => formatValue(fileInstance.cell("M", n), year), + regions[4] => formatValue(fileInstance.cell("O", n), year), + regions[5] => formatValue(fileInstance.cell("Q", n), year), + regions[6] => formatValue(fileInstance.cell("S", n), year) + } + end end def formatValue(val, year) - if val - result = val.to_f + if val + result = val.to_f - if year.to_i < 2017 - result = result / 10000 - end + if year.to_i < 2017 + result = result / 10000 + end - return result.round(2) - end + return result.round(2) + end end def getRecentPriceData(key, products, monthMap) - currentYear = Time.now.strftime("%Y").to_s - - currentMonth = Time.now.strftime("%m") - - monthMap.each{|month, monthNumber| - if monthNumber == currentMonth - currentMonth == month - end - } - - productYearData = products[key]; - - if productYearData[currentYear] - yearKey = currentYear - else - begin - yearKey = productYearData.keys.max{|a,b| a.to_i <=> b.to_i} - rescue - puts productYearData.keys - end - - end - - productMonthData = productYearData[yearKey] - - if productMonthData[currentMonth] - monthKey = currentMonth - else - # begin - # monthKey = productMonthData.keys.max{|a,b| monthMap[a].to_i <=> monthMap[b].to_i} - # rescue - # puts productMonthData.keys - - # dr = { - # "апрель" => "dsad", - # "май" => "dasd" - # } - # dr.keys.max{|a,b| monthMap[a] <=> monthMap[b]} - # end - monthKey = productMonthData.keys.max{|a,b| monthMap[a].to_i <=> monthMap[b].to_i} - end - - return { - "price" => productMonthData[monthKey]["Minsk"], - "year" => yearKey, - "month" => monthKey, - "product" => key - } + currentYear = Time.now.strftime("%Y").to_s + + currentMonth = Time.now.strftime("%m") + + monthMap.each { |month, monthNumber| + if monthNumber == currentMonth + currentMonth == month + end + } + + productYearData = products[key]; + + if productYearData[currentYear] + yearKey = currentYear + else + begin + yearKey = productYearData.keys.max{ |a,b| a.to_i <=> b.to_i} + rescue + puts productYearData.keys + end + + end + + productMonthData = productYearData[yearKey] + + if productMonthData[currentMonth] + monthKey = currentMonth + else + monthKey = productMonthData.keys.max{ |a,b| monthMap[a].to_i <=> monthMap[b].to_i} + end + + return { + "price" => productMonthData[monthKey]["Minsk"], + "year" => yearKey, + "month" => monthKey, + "product" => key + } end def getMinPrice(hash) - result = Hash.new - minYearPrice = 9999999999999 - - hash.each{|year, yearHash| - minMonthPrice = 9999999999999 - yearHash.each{|month, monthHash| - price = monthHash['Minsk'] - if !price - next - end - if(price < minMonthPrice) - minMonthPrice = price - result['month'] = month - end - } - if(minMonthPrice < minYearPrice) - minYearPrice = minMonthPrice - result['year'] = year - result['price'] = minYearPrice - end + result = Hash.new + minYearPrice = 9999999999999 + + hash.each { |year, yearHash| + minMonthPrice = 9999999999999 + yearHash.each { |month, monthHash| + price = monthHash['Minsk'] + if !price + next + end + if(price < minMonthPrice) + minMonthPrice = price + result['month'] = month + end } - return result + if(minMonthPrice < minYearPrice) + minYearPrice = minMonthPrice + result['year'] = year + result['price'] = minYearPrice + end + } + return result end def getMaxPrice(hash) - result = Hash.new - maxYearPrice = 0 - - hash.each{|year, yearHash| - maxMonthPrice = 0 - yearHash.each{|month, monthHash| - price = monthHash['Minsk'] - if !price - next - end - if(price > maxMonthPrice) - maxMonthPrice = price - result['month'] = month - end - } - if(maxMonthPrice > maxYearPrice) - maxYearPrice = maxMonthPrice - result['year'] = year - result['price'] = maxYearPrice - end + result = Hash.new + maxYearPrice = 0 + + hash.each { |year, yearHash| + maxMonthPrice = 0 + yearHash.each { |month, monthHash| + price = monthHash['Minsk'] + if !price + next + end + if(price > maxMonthPrice) + maxMonthPrice = price + result['month'] = month + end } - return result + if(maxMonthPrice > maxYearPrice) + maxYearPrice = maxMonthPrice + result['year'] = year + result['price'] = maxYearPrice + end + } + return result end def getSimilarPriceProducts(data, products) - result = [] - price = data["price"] - year = data["year"] - month = data["month"] - originProduct = data["product"] - - products.each{|product, productData| - if !productData[year] - next - end - if !productData[year][month] - next - end - if productData[year][month]["Minsk"] == price && product != originProduct - result.push(product) - end - } - return result + result = [] + price = data["price"] + year = data["year"] + month = data["month"] + originProduct = data["product"] + + products.each { |product, productData| + if !productData[year] + next + end + if !productData[year][month] + next + end + if productData[year][month]["Minsk"] == price && product != originProduct + result.push(product) + end + } + return result end def main - monthMap = { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12, - } - - regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - products = Hash.new - filePaths = Dir["./data/*"] - - - filePaths.each{ |filePath| - fileInstance = getFileInstance(filePath) - - if fileInstance - fetchProductsData(fileInstance, products, regions) - end - } - - while true - - puts "What price are you looking for?" - - word = gets.chomp.downcase - - keys = findKeys(word, products) - - if keys.length == 0 - puts word.capitalize + " can not be found in database" - else - keys.each{|key| - puts "" - recentPriceData = getRecentPriceData(key, products, monthMap) - puts key.capitalize + " is " + recentPriceData["price"].to_s + " BYN in Minsk these days." - minPrice = getMinPrice(products[key]) - puts "Lowest was on " + minPrice["year"] + "/" + monthMap[minPrice["month"]].to_s + " at price " + minPrice["price"].to_s + " BYN" - maxPrice = getMaxPrice(products[key]) - puts "Maximum was on " + maxPrice["year"] + "/" + monthMap[maxPrice["month"]].to_s + " at price " + maxPrice["price"].to_s + " BYN" - - similarProducts = getSimilarPriceProducts(recentPriceData, products) - - if similarProducts.length == 0 - puts "No products for similar price" - else - puts "For similar price you also can afford" - puts similarProducts - end - - } - end + monthMap = { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12, + } + + regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + products = Hash.new + filePaths = Dir["./data/*"] + + filePaths.each { |filePath| + fileInstance = getFileInstance(filePath) + if fileInstance + fetchProductsData(fileInstance, products, regions) + end + } + + while true + puts "What price are you looking for?" + word = gets.chomp.downcase + keys = findKeys(word, products) + if keys.length == 0 + puts word.capitalize + " can not be found in database" + else + keys.each { |key| + recentPriceData = getRecentPriceData(key, products, monthMap) + puts "\n" + key.capitalize + " is " + recentPriceData["price"].to_s + " BYN in Minsk these days." + minPrice = getMinPrice(products[key]) + puts "Lowest was on " + minPrice["year"] + "/" + monthMap[minPrice["month"]].to_s + " at price " + minPrice["price"].to_s + " BYN" + maxPrice = getMaxPrice(products[key]) + puts "Maximum was on " + maxPrice["year"] + "/" + monthMap[maxPrice["month"]].to_s + " at price " + maxPrice["price"].to_s + " BYN" + similarProducts = getSimilarPriceProducts(recentPriceData, products) + if similarProducts.empty? == 0 + puts "No products for similar price" + else + puts "For similar price you also can afford" + puts similarProducts + end + } end + end end -main() \ No newline at end of file +main From ee8233655484d236788e6d051658052686b3d257 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 00:19:33 +0300 Subject: [PATCH 03/22] Update run.rb fix #2 --- Hryvicki Kirill/0/run.rb | 184 +++++++++++++++++++-------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index dce8e97..32b36df 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -3,21 +3,21 @@ def getFileInstance(filePath) ext = filePath.split(".")[2] - fileInstance = nil + file_instance = nil if ext == "xls" || ext == "xlsx" - fileInstance = Roo::Spreadsheet.open(filePath) + file_instance = Roo::Spreadsheet.open(filePath) case ext when "xls" - fileInstance = Roo::Excel.new(filePath) + file_instance = Roo::Excel.new(filePath) when "xlsx" - fileInstance = Roo::Excelx.new(filePath) + file_instance = Roo::Excelx.new(filePath) end else - puts "unsupported file type: " + filePath + puts 'unsupported file type: ' + filePath end - return fileInstance + return file_instance end def findKeys(word, products) @@ -31,17 +31,17 @@ def findKeys(word, products) return result end -def fetchProductsData(fileInstance, products, regions) +def fetchProductsData(file_instance, products, regions) - for n in 9..fileInstance.last_row - if fileInstance.cell("E", n) == nil + for n in 9..file_instance.last_row + if file_instance.cell("E", n) == nil next end - year = fileInstance.cell("A", 3).split(" ")[2] - month = fileInstance.cell("A", 3).split(" ")[1] + year = file_instance.cell("A", 3).split(" ")[2] + month = file_instance.cell("A", 3).split(" ")[1] - key = fileInstance.cell("A", n).strip.downcase + key = file_instance.cell("A", n).strip.downcase if !products[key] products[key] = Hash.new @@ -51,13 +51,13 @@ def fetchProductsData(fileInstance, products, regions) end products[key][year][month] = { - regions[0] => formatValue(fileInstance.cell("G", n), year), - regions[1] => formatValue(fileInstance.cell("I", n), year), - regions[2] => formatValue(fileInstance.cell("K", n), year), - regions[3] => formatValue(fileInstance.cell("M", n), year), - regions[4] => formatValue(fileInstance.cell("O", n), year), - regions[5] => formatValue(fileInstance.cell("Q", n), year), - regions[6] => formatValue(fileInstance.cell("S", n), year) + regions[0] => formatValue(file_instance.cell("G", n), year), + regions[1] => formatValue(file_instance.cell("I", n), year), + regions[2] => formatValue(file_instance.cell("K", n), year), + regions[3] => formatValue(file_instance.cell("M", n), year), + regions[4] => formatValue(file_instance.cell("O", n), year), + regions[5] => formatValue(file_instance.cell("Q", n), year), + regions[6] => formatValue(file_instance.cell("S", n), year) } end end @@ -74,66 +74,60 @@ def formatValue(val, year) end end -def getRecentPriceData(key, products, monthMap) - currentYear = Time.now.strftime("%Y").to_s +def getRecentPriceData(key, products, month_map) + current_year = Time.now.strftime("%Y").to_s + current_month = Time.now.strftime("%m") - currentMonth = Time.now.strftime("%m") - - monthMap.each { |month, monthNumber| - if monthNumber == currentMonth - currentMonth == month + month_map.each { |month, month_number| + if month_number == current_month + current_month == month end } - productYearData = products[key]; + product_year_data = products[key]; - if productYearData[currentYear] - yearKey = currentYear + if product_year_data[current_year] + year_key = current_year else - begin - yearKey = productYearData.keys.max{ |a,b| a.to_i <=> b.to_i} - rescue - puts productYearData.keys - end - + year_key = product_year_data.keys.max{ |a,b| a.to_i <=> b.to_i} end - productMonthData = productYearData[yearKey] + product_month_data = product_year_data[year_key] - if productMonthData[currentMonth] - monthKey = currentMonth + if product_month_data[current_month] + month_key = current_month else - monthKey = productMonthData.keys.max{ |a,b| monthMap[a].to_i <=> monthMap[b].to_i} + month_key = product_month_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i} end return { - "price" => productMonthData[monthKey]["Minsk"], - "year" => yearKey, - "month" => monthKey, + "price" => product_month_data[month_key]["Minsk"], + "year" => year_key, + "month" => month_key, "product" => key } end def getMinPrice(hash) result = Hash.new - minYearPrice = 9999999999999 + min_year_price = 9999999999999 - hash.each { |year, yearHash| - minMonthPrice = 9999999999999 - yearHash.each { |month, monthHash| - price = monthHash['Minsk'] + hash.each { |year, year_hash| + min_month_price = 9999999999999 + year_hash.each { |month, month_hash| + price = month_hash['Minsk'] if !price next end - if(price < minMonthPrice) - minMonthPrice = price + if(price < min_month_price) + min_month_price = price result['month'] = month end } - if(minMonthPrice < minYearPrice) - minYearPrice = minMonthPrice + if(min_month_price < min_year_price) + min_year_price = min_month_price result['year'] = year - result['price'] = minYearPrice + result['price'] = min_year_price end } return result @@ -141,24 +135,24 @@ def getMinPrice(hash) def getMaxPrice(hash) result = Hash.new - maxYearPrice = 0 + max_year_price = 0 - hash.each { |year, yearHash| - maxMonthPrice = 0 - yearHash.each { |month, monthHash| - price = monthHash['Minsk'] + hash.each { |year, year_hash| + max_month_price = 0 + year_hash.each { |month, month_hash| + price = month_hash['Minsk'] if !price next end - if(price > maxMonthPrice) - maxMonthPrice = price + if(price > max_month_price) + max_month_price = price result['month'] = month end } - if(maxMonthPrice > maxYearPrice) - maxYearPrice = maxMonthPrice + if(max_month_price > max_year_price) + max_year_price = max_month_price result['year'] = year - result['price'] = maxYearPrice + result['price'] = max_year_price end } return result @@ -169,16 +163,16 @@ def getSimilarPriceProducts(data, products) price = data["price"] year = data["year"] month = data["month"] - originProduct = data["product"] + origin_product = data["product"] - products.each { |product, productData| - if !productData[year] + products.each { |product, product_data| + if !product_data[year] next end - if !productData[year][month] + if !product_data[year][month] next end - if productData[year][month]["Minsk"] == price && product != originProduct + if product_data[year][month]["Minsk"] == price && product != origin_product result.push(product) end } @@ -186,7 +180,7 @@ def getSimilarPriceProducts(data, products) end def main - monthMap = { + month_map = { 'январь' => 1, 'февраль' => 2, 'март' => 3, @@ -203,37 +197,43 @@ def main regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] products = Hash.new - filePaths = Dir["./data/*"] + file_paths = Dir["./data/*"] - filePaths.each { |filePath| - fileInstance = getFileInstance(filePath) - if fileInstance - fetchProductsData(fileInstance, products, regions) + file_paths.each { |filePath| + file_instance = getFileInstance(filePath) + if file_instance + fetchProductsData(file_instance, products, regions) end } - while true - puts "What price are you looking for?" - word = gets.chomp.downcase + loop do + puts 'What price are you looking for?' + word = gets.chomp.downcase #.encode("UTF-8") keys = findKeys(word, products) - if keys.length == 0 - puts word.capitalize + " can not be found in database" + if keys.empty? + puts word.capitalize + ' can not be found in database' else keys.each { |key| - recentPriceData = getRecentPriceData(key, products, monthMap) - puts "\n" + key.capitalize + " is " + recentPriceData["price"].to_s + " BYN in Minsk these days." - minPrice = getMinPrice(products[key]) - puts "Lowest was on " + minPrice["year"] + "/" + monthMap[minPrice["month"]].to_s + " at price " + minPrice["price"].to_s + " BYN" - maxPrice = getMaxPrice(products[key]) - puts "Maximum was on " + maxPrice["year"] + "/" + monthMap[maxPrice["month"]].to_s + " at price " + maxPrice["price"].to_s + " BYN" - similarProducts = getSimilarPriceProducts(recentPriceData, products) - if similarProducts.empty? == 0 - puts "No products for similar price" - else - puts "For similar price you also can afford" - puts similarProducts - end - } + recent_price_data = getRecentPriceData(key, products, month_map) + puts '' + puts key.capitalize + ' is ' + recent_price_data["price"].to_s + ' BYN in Minsk these days.' + + min_price = getMinPrice(products[key]) + puts 'Lowest was on ' + min_price["year"] + '/' + month_map[min_price["month"]].to_s + + ' at price ' + min_price["price"].to_s + ' BYN' + + max_price = getMaxPrice(products[key]) + puts 'Maximum was on ' + max_price["year"] + '/' + month_map[max_price["month"]].to_s + + ' at price ' + max_price["price"].to_s + ' BYN' + + similar_products = getSimilarPriceProducts(recent_price_data, products) + if similar_products.empty? + puts 'No products for similar price' + else + puts 'For similar price you also can afford' + puts similar_products + end + } end end end From 2f58ca25c5bb37bd1afc9ee54b63dc4f846d390b Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 00:44:59 +0300 Subject: [PATCH 04/22] Update run.rb --- Hryvicki Kirill/0/run.rb | 184 +++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 106 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 32b36df..8f7f320 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -1,20 +1,20 @@ require 'roo' require 'roo-xls' -def getFileInstance(filePath) - ext = filePath.split(".")[2] +def getFileInstance(file_path) + ext = file_path.split('.')[2] file_instance = nil - if ext == "xls" || ext == "xlsx" - file_instance = Roo::Spreadsheet.open(filePath) - case ext - when "xls" - file_instance = Roo::Excel.new(filePath) - when "xlsx" - file_instance = Roo::Excelx.new(filePath) - end + if ext == 'xls' || ext == 'xlsx' + file_instance = Roo::Spreadsheet.open(file_path) + case ext + when 'xls' + file_instance = Roo::Excel.new(file_path) + when 'xlsx' + file_instance = Roo::Excelx.new(file_path) + end else - puts 'unsupported file type: ' + filePath + puts 'unsupported file type: ' + file_path end return file_instance @@ -22,66 +22,49 @@ def getFileInstance(filePath) def findKeys(word, products) result = [] - products.keys.each { |key| - if(key.include?(word)) - result.push(key) - end + result.push(key) if(key.include?(word)) } return result end def fetchProductsData(file_instance, products, regions) - for n in 9..file_instance.last_row - if file_instance.cell("E", n) == nil - next - end - - year = file_instance.cell("A", 3).split(" ")[2] - month = file_instance.cell("A", 3).split(" ")[1] - - key = file_instance.cell("A", n).strip.downcase - - if !products[key] - products[key] = Hash.new - end - if !products[key][year] - products[key][year] = Hash.new - end - - products[key][year][month] = { - regions[0] => formatValue(file_instance.cell("G", n), year), - regions[1] => formatValue(file_instance.cell("I", n), year), - regions[2] => formatValue(file_instance.cell("K", n), year), - regions[3] => formatValue(file_instance.cell("M", n), year), - regions[4] => formatValue(file_instance.cell("O", n), year), - regions[5] => formatValue(file_instance.cell("Q", n), year), - regions[6] => formatValue(file_instance.cell("S", n), year) - } + next if file_instance.cell('E', n) == nil + + year = file_instance.cell('A', 3).split(' ')[2] + month = file_instance.cell('A', 3).split(' ')[1] + key = file_instance.cell('A', n).strip.downcase + + products[key] = {} if !products[key] + products[key][year] = {} if !products[key][year] + + products[key][year][month] = { + regions[0] => formatValue(file_instance.cell('G', n), year), + regions[1] => formatValue(file_instance.cell('I', n), year), + regions[2] => formatValue(file_instance.cell('K', n), year), + regions[3] => formatValue(file_instance.cell('M', n), year), + regions[4] => formatValue(file_instance.cell('O', n), year), + regions[5] => formatValue(file_instance.cell('Q', n), year), + regions[6] => formatValue(file_instance.cell('S', n), year) + } end end def formatValue(val, year) if val - result = val.to_f - - if year.to_i < 2017 - result = result / 10000 - end - - return result.round(2) + result = val.to_f + result = result / 10000 if year.to_i < 2017 + return result.round(2) end end def getRecentPriceData(key, products, month_map) - current_year = Time.now.strftime("%Y").to_s - current_month = Time.now.strftime("%m") + current_year = Time.now.strftime('%Y').to_s + current_month = Time.now.strftime('%m') month_map.each { |month, month_number| - if month_number == current_month - current_month == month - end + current_month = month if month_number == current_month } product_year_data = products[key]; @@ -101,80 +84,71 @@ def getRecentPriceData(key, products, month_map) end return { - "price" => product_month_data[month_key]["Minsk"], - "year" => year_key, - "month" => month_key, - "product" => key + 'price' => product_month_data[month_key]['Minsk'], + 'year' => year_key, + 'month' => month_key, + 'product' => key } end def getMinPrice(hash) - result = Hash.new + result = {} min_year_price = 9999999999999 - hash.each { |year, year_hash| + hash.each do |year, year_hash| min_month_price = 9999999999999 - year_hash.each { |month, month_hash| + year_hash.each do |month, month_hash| price = month_hash['Minsk'] - if !price - next - end - if(price < min_month_price) + next if !price + if price < min_month_price min_month_price = price result['month'] = month end - } - if(min_month_price < min_year_price) + end + if min_month_price < min_year_price min_year_price = min_month_price result['year'] = year result['price'] = min_year_price end - } + end return result end def getMaxPrice(hash) - result = Hash.new + result = {} max_year_price = 0 - hash.each { |year, year_hash| + hash.each do |year, year_hash| max_month_price = 0 - year_hash.each { |month, month_hash| + year_hash.each do |month, month_hash| price = month_hash['Minsk'] - if !price - next - end - if(price > max_month_price) + next if !price + + if price > max_month_price max_month_price = price result['month'] = month end - } - if(max_month_price > max_year_price) + end + if max_month_price > max_year_price max_year_price = max_month_price result['year'] = year result['price'] = max_year_price end - } + end return result end def getSimilarPriceProducts(data, products) result = [] - price = data["price"] - year = data["year"] - month = data["month"] - origin_product = data["product"] + price = data['price'] + year = data['year'] + month = data['month'] + origin_product = data['product'] products.each { |product, product_data| - if !product_data[year] - next - end - if !product_data[year][month] - next - end - if product_data[year][month]["Minsk"] == price && product != origin_product - result.push(product) - end + next if !product_data[year] + next if !product_data[year][month] + result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product } return result end @@ -196,35 +170,33 @@ def main } regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - products = Hash.new - file_paths = Dir["./data/*"] + products = {} + file_paths = Dir['./data/*'] - file_paths.each { |filePath| - file_instance = getFileInstance(filePath) - if file_instance - fetchProductsData(file_instance, products, regions) - end - } + file_paths.each do |file_path| + file_instance = getFileInstance(file_path) + fetchProductsData(file_instance, products, regions) if file_instance + end loop do puts 'What price are you looking for?' - word = gets.chomp.downcase #.encode("UTF-8") + word = gets.chomp.downcase # .encode('UTF-8') keys = findKeys(word, products) if keys.empty? puts word.capitalize + ' can not be found in database' else - keys.each { |key| + keys.each do |key| recent_price_data = getRecentPriceData(key, products, month_map) puts '' - puts key.capitalize + ' is ' + recent_price_data["price"].to_s + ' BYN in Minsk these days.' + puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' min_price = getMinPrice(products[key]) - puts 'Lowest was on ' + min_price["year"] + '/' + month_map[min_price["month"]].to_s + - ' at price ' + min_price["price"].to_s + ' BYN' + puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + + ' at price ' + min_price['price'].to_s + ' BYN' max_price = getMaxPrice(products[key]) - puts 'Maximum was on ' + max_price["year"] + '/' + month_map[max_price["month"]].to_s + - ' at price ' + max_price["price"].to_s + ' BYN' + puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + + ' at price ' + max_price['price'].to_s + ' BYN' similar_products = getSimilarPriceProducts(recent_price_data, products) if similar_products.empty? @@ -233,7 +205,7 @@ def main puts 'For similar price you also can afford' puts similar_products end - } + end end end end From f282d324b26cb4a9b9b2f90744c0a14fc68dee01 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 01:04:17 +0300 Subject: [PATCH 05/22] Create rub.rb fix4 --- Hryvicki Kirill/0/rub.rb | 213 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 Hryvicki Kirill/0/rub.rb diff --git a/Hryvicki Kirill/0/rub.rb b/Hryvicki Kirill/0/rub.rb new file mode 100644 index 0000000..8f7f320 --- /dev/null +++ b/Hryvicki Kirill/0/rub.rb @@ -0,0 +1,213 @@ +require 'roo' +require 'roo-xls' + +def getFileInstance(file_path) + ext = file_path.split('.')[2] + file_instance = nil + + if ext == 'xls' || ext == 'xlsx' + file_instance = Roo::Spreadsheet.open(file_path) + case ext + when 'xls' + file_instance = Roo::Excel.new(file_path) + when 'xlsx' + file_instance = Roo::Excelx.new(file_path) + end + else + puts 'unsupported file type: ' + file_path + end + + return file_instance +end + +def findKeys(word, products) + result = [] + products.keys.each { |key| + result.push(key) if(key.include?(word)) + } + return result +end + +def fetchProductsData(file_instance, products, regions) + for n in 9..file_instance.last_row + next if file_instance.cell('E', n) == nil + + year = file_instance.cell('A', 3).split(' ')[2] + month = file_instance.cell('A', 3).split(' ')[1] + key = file_instance.cell('A', n).strip.downcase + + products[key] = {} if !products[key] + products[key][year] = {} if !products[key][year] + + products[key][year][month] = { + regions[0] => formatValue(file_instance.cell('G', n), year), + regions[1] => formatValue(file_instance.cell('I', n), year), + regions[2] => formatValue(file_instance.cell('K', n), year), + regions[3] => formatValue(file_instance.cell('M', n), year), + regions[4] => formatValue(file_instance.cell('O', n), year), + regions[5] => formatValue(file_instance.cell('Q', n), year), + regions[6] => formatValue(file_instance.cell('S', n), year) + } + end +end + +def formatValue(val, year) + if val + result = val.to_f + result = result / 10000 if year.to_i < 2017 + return result.round(2) + end +end + +def getRecentPriceData(key, products, month_map) + current_year = Time.now.strftime('%Y').to_s + current_month = Time.now.strftime('%m') + + month_map.each { |month, month_number| + current_month = month if month_number == current_month + } + + product_year_data = products[key]; + + if product_year_data[current_year] + year_key = current_year + else + year_key = product_year_data.keys.max{ |a,b| a.to_i <=> b.to_i} + end + + product_month_data = product_year_data[year_key] + + if product_month_data[current_month] + month_key = current_month + else + month_key = product_month_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i} + end + + return { + 'price' => product_month_data[month_key]['Minsk'], + 'year' => year_key, + 'month' => month_key, + 'product' => key + } +end + +def getMinPrice(hash) + result = {} + min_year_price = 9999999999999 + + hash.each do |year, year_hash| + min_month_price = 9999999999999 + year_hash.each do |month, month_hash| + price = month_hash['Minsk'] + next if !price + if price < min_month_price + min_month_price = price + result['month'] = month + end + end + if min_month_price < min_year_price + min_year_price = min_month_price + result['year'] = year + result['price'] = min_year_price + end + end + return result +end + +def getMaxPrice(hash) + result = {} + max_year_price = 0 + + hash.each do |year, year_hash| + max_month_price = 0 + year_hash.each do |month, month_hash| + price = month_hash['Minsk'] + next if !price + + if price > max_month_price + max_month_price = price + result['month'] = month + end + end + if max_month_price > max_year_price + max_year_price = max_month_price + result['year'] = year + result['price'] = max_year_price + end + end + return result +end + +def getSimilarPriceProducts(data, products) + result = [] + price = data['price'] + year = data['year'] + month = data['month'] + origin_product = data['product'] + + products.each { |product, product_data| + next if !product_data[year] + next if !product_data[year][month] + result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product + } + return result +end + +def main + month_map = { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12, + } + + regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + products = {} + file_paths = Dir['./data/*'] + + file_paths.each do |file_path| + file_instance = getFileInstance(file_path) + fetchProductsData(file_instance, products, regions) if file_instance + end + + loop do + puts 'What price are you looking for?' + word = gets.chomp.downcase # .encode('UTF-8') + keys = findKeys(word, products) + if keys.empty? + puts word.capitalize + ' can not be found in database' + else + keys.each do |key| + recent_price_data = getRecentPriceData(key, products, month_map) + puts '' + puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' + + min_price = getMinPrice(products[key]) + puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + + ' at price ' + min_price['price'].to_s + ' BYN' + + max_price = getMaxPrice(products[key]) + puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + + ' at price ' + max_price['price'].to_s + ' BYN' + + similar_products = getSimilarPriceProducts(recent_price_data, products) + if similar_products.empty? + puts 'No products for similar price' + else + puts 'For similar price you also can afford' + puts similar_products + end + end + end + end +end + +main From f427a7e344d0b7892c0c370400b73582ac964a53 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 11:20:12 +0300 Subject: [PATCH 06/22] Update rub.rb 5 --- Hryvicki Kirill/0/rub.rb | 95 +++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/Hryvicki Kirill/0/rub.rb b/Hryvicki Kirill/0/rub.rb index 8f7f320..6034419 100644 --- a/Hryvicki Kirill/0/rub.rb +++ b/Hryvicki Kirill/0/rub.rb @@ -1,7 +1,7 @@ require 'roo' require 'roo-xls' -def getFileInstance(file_path) +def get_file_instance(file_path) ext = file_path.split('.')[2] file_instance = nil @@ -16,19 +16,18 @@ def getFileInstance(file_path) else puts 'unsupported file type: ' + file_path end - - return file_instance + file_instance end -def findKeys(word, products) +def find_keys(word, products) result = [] products.keys.each { |key| result.push(key) if(key.include?(word)) } - return result + result end -def fetchProductsData(file_instance, products, regions) +def fetch_products_data(file_instance, products, regions) for n in 9..file_instance.last_row next if file_instance.cell('E', n) == nil @@ -36,30 +35,40 @@ def fetchProductsData(file_instance, products, regions) month = file_instance.cell('A', 3).split(' ')[1] key = file_instance.cell('A', n).strip.downcase - products[key] = {} if !products[key] - products[key][year] = {} if !products[key][year] + products[key] = {} unless products[key] + products[key][year] = {} unless products[key][year] products[key][year][month] = { - regions[0] => formatValue(file_instance.cell('G', n), year), - regions[1] => formatValue(file_instance.cell('I', n), year), - regions[2] => formatValue(file_instance.cell('K', n), year), - regions[3] => formatValue(file_instance.cell('M', n), year), - regions[4] => formatValue(file_instance.cell('O', n), year), - regions[5] => formatValue(file_instance.cell('Q', n), year), - regions[6] => formatValue(file_instance.cell('S', n), year) + regions[0] => format_value(file_instance.cell('G', n), year), + regions[1] => format_value(file_instance.cell('I', n), year), + regions[2] => format_value(file_instance.cell('K', n), year), + regions[3] => format_value(file_instance.cell('M', n), year), + regions[4] => format_value(file_instance.cell('O', n), year), + regions[5] => format_value(file_instance.cell('Q', n), year), + regions[6] => format_value(file_instance.cell('S', n), year) } end end -def formatValue(val, year) +def format_value(val, year) if val result = val.to_f - result = result / 10000 if year.to_i < 2017 + result = result / 10_000 if year.to_i < 2017 return result.round(2) end end -def getRecentPriceData(key, products, month_map) +{ + товар => { + год => { + месяц => { + регион => цена + } + } + } +} + +def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s current_month = Time.now.strftime('%m') @@ -91,15 +100,14 @@ def getRecentPriceData(key, products, month_map) } end -def getMinPrice(hash) +def get_min_price(hash) result = {} - min_year_price = 9999999999999 - + min_year_price = 9_999_999_999_999 hash.each do |year, year_hash| - min_month_price = 9999999999999 + min_month_price = 9_999_999_999_999 year_hash.each do |month, month_hash| price = month_hash['Minsk'] - next if !price + next unless price if price < min_month_price min_month_price = price result['month'] = month @@ -111,10 +119,10 @@ def getMinPrice(hash) result['price'] = min_year_price end end - return result + result end -def getMaxPrice(hash) +def get_max_price(hash) result = {} max_year_price = 0 @@ -122,7 +130,7 @@ def getMaxPrice(hash) max_month_price = 0 year_hash.each do |month, month_hash| price = month_hash['Minsk'] - next if !price + next unless price if price > max_month_price max_month_price = price @@ -135,10 +143,10 @@ def getMaxPrice(hash) result['price'] = max_year_price end end - return result + result end -def getSimilarPriceProducts(data, products) +def get_similar_price_products(data, products) result = [] price = data['price'] year = data['year'] @@ -146,11 +154,11 @@ def getSimilarPriceProducts(data, products) origin_product = data['product'] products.each { |product, product_data| - next if !product_data[year] - next if !product_data[year][month] + next unless product_data[year] + next unless product_data[year][month] result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product } - return result + result end def main @@ -168,37 +176,32 @@ def main 'ноябрь' => 11, 'декабрь' => 12, } - regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] products = {} file_paths = Dir['./data/*'] - file_paths.each do |file_path| - file_instance = getFileInstance(file_path) - fetchProductsData(file_instance, products, regions) if file_instance + file_instance = get_file_instance(file_path) + fetch_products_data(file_instance, products, regions) if file_instance end - loop do puts 'What price are you looking for?' word = gets.chomp.downcase # .encode('UTF-8') - keys = findKeys(word, products) + keys = find_keys(word, products) if keys.empty? puts word.capitalize + ' can not be found in database' else keys.each do |key| - recent_price_data = getRecentPriceData(key, products, month_map) + recent_price_data = get_recent_price_data(key, products, month_map) puts '' - puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' + puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' - min_price = getMinPrice(products[key]) - puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + - ' at price ' + min_price['price'].to_s + ' BYN' + min_price = get_min_price(products[key]) + puts 'Lowest was on '+min_price['year']+'/'+month_map[min_price['month']].to_s +' at price '+min_price['price'].to_s+' BYN' - max_price = getMaxPrice(products[key]) - puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + - ' at price ' + max_price['price'].to_s + ' BYN' + max_price = get_max_price(products[key]) + puts 'Maximum was on '+max_price['year']+'/'+month_map[max_price['month']].to_s+' at price '+ max_price['price'].to_s+' BYN' - similar_products = getSimilarPriceProducts(recent_price_data, products) + similar_products = get_similar_price_products(recent_price_data, products) if similar_products.empty? puts 'No products for similar price' else From b3dcf4625304e83fccf457a08344262e3a00b7fd Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 14:04:20 +0300 Subject: [PATCH 07/22] fix6 6 --- Hryvicki Kirill/0/rub.rb | 216 ---------------------------- Hryvicki Kirill/0/run.rb | 299 +++++++++++++++++++++------------------ 2 files changed, 159 insertions(+), 356 deletions(-) delete mode 100644 Hryvicki Kirill/0/rub.rb diff --git a/Hryvicki Kirill/0/rub.rb b/Hryvicki Kirill/0/rub.rb deleted file mode 100644 index 6034419..0000000 --- a/Hryvicki Kirill/0/rub.rb +++ /dev/null @@ -1,216 +0,0 @@ -require 'roo' -require 'roo-xls' - -def get_file_instance(file_path) - ext = file_path.split('.')[2] - file_instance = nil - - if ext == 'xls' || ext == 'xlsx' - file_instance = Roo::Spreadsheet.open(file_path) - case ext - when 'xls' - file_instance = Roo::Excel.new(file_path) - when 'xlsx' - file_instance = Roo::Excelx.new(file_path) - end - else - puts 'unsupported file type: ' + file_path - end - file_instance -end - -def find_keys(word, products) - result = [] - products.keys.each { |key| - result.push(key) if(key.include?(word)) - } - result -end - -def fetch_products_data(file_instance, products, regions) - for n in 9..file_instance.last_row - next if file_instance.cell('E', n) == nil - - year = file_instance.cell('A', 3).split(' ')[2] - month = file_instance.cell('A', 3).split(' ')[1] - key = file_instance.cell('A', n).strip.downcase - - products[key] = {} unless products[key] - products[key][year] = {} unless products[key][year] - - products[key][year][month] = { - regions[0] => format_value(file_instance.cell('G', n), year), - regions[1] => format_value(file_instance.cell('I', n), year), - regions[2] => format_value(file_instance.cell('K', n), year), - regions[3] => format_value(file_instance.cell('M', n), year), - regions[4] => format_value(file_instance.cell('O', n), year), - regions[5] => format_value(file_instance.cell('Q', n), year), - regions[6] => format_value(file_instance.cell('S', n), year) - } - end -end - -def format_value(val, year) - if val - result = val.to_f - result = result / 10_000 if year.to_i < 2017 - return result.round(2) - end -end - -{ - товар => { - год => { - месяц => { - регион => цена - } - } - } -} - -def get_recent_price_data(key, products, month_map) - current_year = Time.now.strftime('%Y').to_s - current_month = Time.now.strftime('%m') - - month_map.each { |month, month_number| - current_month = month if month_number == current_month - } - - product_year_data = products[key]; - - if product_year_data[current_year] - year_key = current_year - else - year_key = product_year_data.keys.max{ |a,b| a.to_i <=> b.to_i} - end - - product_month_data = product_year_data[year_key] - - if product_month_data[current_month] - month_key = current_month - else - month_key = product_month_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i} - end - - return { - 'price' => product_month_data[month_key]['Minsk'], - 'year' => year_key, - 'month' => month_key, - 'product' => key - } -end - -def get_min_price(hash) - result = {} - min_year_price = 9_999_999_999_999 - hash.each do |year, year_hash| - min_month_price = 9_999_999_999_999 - year_hash.each do |month, month_hash| - price = month_hash['Minsk'] - next unless price - if price < min_month_price - min_month_price = price - result['month'] = month - end - end - if min_month_price < min_year_price - min_year_price = min_month_price - result['year'] = year - result['price'] = min_year_price - end - end - result -end - -def get_max_price(hash) - result = {} - max_year_price = 0 - - hash.each do |year, year_hash| - max_month_price = 0 - year_hash.each do |month, month_hash| - price = month_hash['Minsk'] - next unless price - - if price > max_month_price - max_month_price = price - result['month'] = month - end - end - if max_month_price > max_year_price - max_year_price = max_month_price - result['year'] = year - result['price'] = max_year_price - end - end - result -end - -def get_similar_price_products(data, products) - result = [] - price = data['price'] - year = data['year'] - month = data['month'] - origin_product = data['product'] - - products.each { |product, product_data| - next unless product_data[year] - next unless product_data[year][month] - result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product - } - result -end - -def main - month_map = { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12, - } - regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - products = {} - file_paths = Dir['./data/*'] - file_paths.each do |file_path| - file_instance = get_file_instance(file_path) - fetch_products_data(file_instance, products, regions) if file_instance - end - loop do - puts 'What price are you looking for?' - word = gets.chomp.downcase # .encode('UTF-8') - keys = find_keys(word, products) - if keys.empty? - puts word.capitalize + ' can not be found in database' - else - keys.each do |key| - recent_price_data = get_recent_price_data(key, products, month_map) - puts '' - puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' - - min_price = get_min_price(products[key]) - puts 'Lowest was on '+min_price['year']+'/'+month_map[min_price['month']].to_s +' at price '+min_price['price'].to_s+' BYN' - - max_price = get_max_price(products[key]) - puts 'Maximum was on '+max_price['year']+'/'+month_map[max_price['month']].to_s+' at price '+ max_price['price'].to_s+' BYN' - - similar_products = get_similar_price_products(recent_price_data, products) - if similar_products.empty? - puts 'No products for similar price' - else - puts 'For similar price you also can afford' - puts similar_products - end - end - end - end -end - -main diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 8f7f320..8d0f49e 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -1,213 +1,232 @@ require 'roo' require 'roo-xls' -def getFileInstance(file_path) +def get_file_instance(file_path) ext = file_path.split('.')[2] file_instance = nil - if ext == 'xls' || ext == 'xlsx' file_instance = Roo::Spreadsheet.open(file_path) - case ext - when 'xls' - file_instance = Roo::Excel.new(file_path) - when 'xlsx' - file_instance = Roo::Excelx.new(file_path) - end + file_instance = conditiona_load(ext, file_path) else puts 'unsupported file type: ' + file_path end + file_instance +end - return file_instance +def conditiona_load(ext, file_path) + case ext + when 'xls' + sheet = Roo::Excel.new(file_path) + when 'xlsx' + sheet = Roo::Excelx.new(file_path) + end + sheet end -def findKeys(word, products) +def find_keys(word, products) result = [] products.keys.each { |key| - result.push(key) if(key.include?(word)) + result.push(key) if key.include?(word) } - return result + result end -def fetchProductsData(file_instance, products, regions) +def fetch_products_data(file_instance, products, regions) for n in 9..file_instance.last_row next if file_instance.cell('E', n) == nil - year = file_instance.cell('A', 3).split(' ')[2] month = file_instance.cell('A', 3).split(' ')[1] key = file_instance.cell('A', n).strip.downcase + add_product(products, key, year, month, regions, file_instance, n) + end +end - products[key] = {} if !products[key] - products[key][year] = {} if !products[key][year] - +def add_product(products, key, year, month, regions, file_instance, n) + products[key] = {} unless products[key] + products[key][year] = {} unless products[key][year] products[key][year][month] = { - regions[0] => formatValue(file_instance.cell('G', n), year), - regions[1] => formatValue(file_instance.cell('I', n), year), - regions[2] => formatValue(file_instance.cell('K', n), year), - regions[3] => formatValue(file_instance.cell('M', n), year), - regions[4] => formatValue(file_instance.cell('O', n), year), - regions[5] => formatValue(file_instance.cell('Q', n), year), - regions[6] => formatValue(file_instance.cell('S', n), year) + regions[0] => format_value(file_instance.cell('G', n), year), + regions[1] => format_value(file_instance.cell('I', n), year), + regions[2] => format_value(file_instance.cell('K', n), year), + regions[3] => format_value(file_instance.cell('M', n), year), + regions[4] => format_value(file_instance.cell('O', n), year), + regions[5] => format_value(file_instance.cell('Q', n), year), + regions[6] => format_value(file_instance.cell('S', n), year) } - end end -def formatValue(val, year) +def format_value(val, year) if val result = val.to_f - result = result / 10000 if year.to_i < 2017 + result = result / 10_000 if year.to_i < 2017 return result.round(2) end end -def getRecentPriceData(key, products, month_map) +def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s current_month = Time.now.strftime('%m') - - month_map.each { |month, month_number| - current_month = month if month_number == current_month - } - + month_map.each { |month, month_number| current_month = month if month_number == current_month } product_year_data = products[key]; + year_key = get_closest_year(current_year, product_year_data) + product_month_data = product_year_data[year_key] + month_key = get_closest_month(current_month, product_month_data, month_map) + return { 'price' => product_month_data[month_key]['Minsk'], 'year' => year_key, 'month' => month_key, 'product' => key } +end - if product_year_data[current_year] +def get_closest_year(current_year, product_data) + if product_data[current_year] year_key = current_year else - year_key = product_year_data.keys.max{ |a,b| a.to_i <=> b.to_i} + year_key = product_data.keys.max{ |a,b| a.to_i <=> b.to_i } end + year_key +end - product_month_data = product_year_data[year_key] - - if product_month_data[current_month] +def get_closest_month(current_month, product_data, month_map) + if product_data[current_month] month_key = current_month else - month_key = product_month_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i} + month_key = product_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i } end - - return { - 'price' => product_month_data[month_key]['Minsk'], - 'year' => year_key, - 'month' => month_key, - 'product' => key - } + month_key end -def getMinPrice(hash) +def get_min_price(hash) + min_year_price = 9_999_999_999_999 result = {} - min_year_price = 9999999999999 - hash.each do |year, year_hash| - min_month_price = 9999999999999 - year_hash.each do |month, month_hash| - price = month_hash['Minsk'] - next if !price - if price < min_month_price - min_month_price = price - result['month'] = month - end - end - if min_month_price < min_year_price - min_year_price = min_month_price - result['year'] = year - result['price'] = min_year_price + min_month_data = find_min_month(year_hash) + min_year_data = find_min_year(year, min_month_data, min_year_price) + min_year_price = min_year_data['price'] + result = actual_data(result, min_year_data) + end + result +end + +def find_min_month(year_hash, min_month_price = 9_999_999_999_999, min_month = nil) + year_hash.each do |month, month_hash| + price = month_hash['Minsk'] + next unless price + if price < min_month_price + min_month_price = price + min_month = month end end - return result + { 'price' => min_month_price, 'month' => min_month } end -def getMaxPrice(hash) +def find_min_year(year, min_month_data, min_year_price) result = {} - max_year_price = 0 + min_month_price = min_month_data["price"] + result['month'] = min_month_data["month"] + if min_month_price < min_year_price + result['price'] = min_month_price + result['year'] = year + end + result +end +def get_max_price(hash) + max_year_price = 0 + result = {} hash.each do |year, year_hash| - max_month_price = 0 - year_hash.each do |month, month_hash| - price = month_hash['Minsk'] - next if !price - - if price > max_month_price - max_month_price = price - result['month'] = month - end - end - if max_month_price > max_year_price - max_year_price = max_month_price - result['year'] = year - result['price'] = max_year_price + max_month_data = find_max_month(year_hash) + max_year_data = find_max_year(year, max_month_data, max_year_price) + max_year_price = max_year_data['price'] + result = actual_data(result, max_year_data) + end + result +end + +def find_max_month(year_hash, max_month_price = 0, max_month = nil) + year_hash.each do |month, month_hash| + price = month_hash['Minsk'] + next unless price + if price > max_month_price + max_month_price = price + max_month = month end end - return result + { 'price' => max_month_price, 'month' => max_month } end -def getSimilarPriceProducts(data, products) +def find_max_year(year, max_month_data, max_year_price) + result = {} + max_month_price = max_month_data["price"] + result['month'] = max_month_data["month"] + if max_month_price > max_year_price + result['price'] = max_month_price + result['year'] = year + end + result +end + +def actual_data(result, year_data) + result["month"] = year_data["month"] + result["price"] = year_data["price"] if year_data["price"] + result["year"] = year_data["year"] if year_data["year"] + result +end + +def get_similar_price_products(data, products) result = [] price = data['price'] year = data['year'] month = data['month'] origin_product = data['product'] - products.each { |product, product_data| - next if !product_data[year] - next if !product_data[year][month] + next unless product_data[year] + next unless product_data[year][month] result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product } - return result -end - -def main - month_map = { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12, - } - - regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - products = {} - file_paths = Dir['./data/*'] - - file_paths.each do |file_path| - file_instance = getFileInstance(file_path) - fetchProductsData(file_instance, products, regions) if file_instance - end + result +end + - loop do - puts 'What price are you looking for?' - word = gets.chomp.downcase # .encode('UTF-8') - keys = findKeys(word, products) - if keys.empty? - puts word.capitalize + ' can not be found in database' - else - keys.each do |key| - recent_price_data = getRecentPriceData(key, products, month_map) - puts '' - puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' - - min_price = getMinPrice(products[key]) - puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + - ' at price ' + min_price['price'].to_s + ' BYN' - - max_price = getMaxPrice(products[key]) - puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + - ' at price ' + max_price['price'].to_s + ' BYN' - - similar_products = getSimilarPriceProducts(recent_price_data, products) - if similar_products.empty? - puts 'No products for similar price' - else - puts 'For similar price you also can afford' - puts similar_products - end +month_map = { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12, +} +regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] +products = {} +file_paths = Dir['./data/*'] +file_paths.each do |file_path| + file_instance = get_file_instance(file_path) + fetch_products_data(file_instance, products, regions) if file_instance +end +loop do + puts 'What price are you looking for?' + word = gets.chomp.downcase.encode('UTF-8') + keys = find_keys(word, products) + if keys.empty? + puts word.capitalize + ' can not be found in database' + else + keys.each do |key| + recent_price_data = get_recent_price_data(key, products, month_map) + puts '' + puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' + min_price = get_min_price(products[key]) + puts 'Lowest was on '+min_price['year']+'/'+month_map[min_price['month']].to_s+' at price '+min_price['price'].to_s+' BYN' + max_price = get_max_price(products[key]) + puts 'Maximum was on '+max_price['year']+'/'+month_map[max_price['month']].to_s+' at price '+max_price['price'].to_s+' BYN' + similar_products = get_similar_price_products(recent_price_data, products) + if similar_products.empty? + puts 'No products for similar price' + else + puts 'For similar price you also can afford' + puts similar_products end end end end - -main From 56858bdd8e4247666909900c666e9720a63830e8 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 14:27:58 +0300 Subject: [PATCH 08/22] Update run.rb 6 --- Hryvicki Kirill/0/run.rb | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 8d0f49e..5349762 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -78,7 +78,7 @@ def get_closest_year(current_year, product_data) if product_data[current_year] year_key = current_year else - year_key = product_data.keys.max{ |a,b| a.to_i <=> b.to_i } + year_key = product_data.keys.max { |a, b| a.to_i <=> b.to_i } end year_key end @@ -87,7 +87,7 @@ def get_closest_month(current_month, product_data, month_map) if product_data[current_month] month_key = current_month else - month_key = product_data.keys.max{ |a,b| month_map[a].to_i <=> month_map[b].to_i } + month_key = product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } end month_key end @@ -107,7 +107,7 @@ def get_min_price(hash) def find_min_month(year_hash, min_month_price = 9_999_999_999_999, min_month = nil) year_hash.each do |month, month_hash| price = month_hash['Minsk'] - next unless price + next unless price if price < min_month_price min_month_price = price min_month = month @@ -118,8 +118,8 @@ def find_min_month(year_hash, min_month_price = 9_999_999_999_999, min_month = n def find_min_year(year, min_month_data, min_year_price) result = {} - min_month_price = min_month_data["price"] - result['month'] = min_month_data["month"] + min_month_price = min_month_data['price'] + result['month'] = min_month_data['month'] if min_month_price < min_year_price result['price'] = min_month_price result['year'] = year @@ -142,7 +142,7 @@ def get_max_price(hash) def find_max_month(year_hash, max_month_price = 0, max_month = nil) year_hash.each do |month, month_hash| price = month_hash['Minsk'] - next unless price + next unless price if price > max_month_price max_month_price = price max_month = month @@ -153,8 +153,8 @@ def find_max_month(year_hash, max_month_price = 0, max_month = nil) def find_max_year(year, max_month_data, max_year_price) result = {} - max_month_price = max_month_data["price"] - result['month'] = max_month_data["month"] + max_month_price = max_month_data['price'] + result['month'] = max_month_data['month'] if max_month_price > max_year_price result['price'] = max_month_price result['year'] = year @@ -163,18 +163,22 @@ def find_max_year(year, max_month_data, max_year_price) end def actual_data(result, year_data) - result["month"] = year_data["month"] - result["price"] = year_data["price"] if year_data["price"] - result["year"] = year_data["year"] if year_data["year"] + result['month'] = year_data['month'] + result['price'] = year_data['price'] if year_data['price'] + result['year'] = year_data['year'] if year_data['year'] result end def get_similar_price_products(data, products) - result = [] price = data['price'] year = data['year'] month = data['month'] origin_product = data['product'] + return form_similar_products_array(products, price, year, month, origin_product) +end + +def form_similar_products_array(products, price, year, month, origin_product) + result = [] products.each { |product, product_data| next unless product_data[year] next unless product_data[year][month] @@ -183,7 +187,6 @@ def get_similar_price_products(data, products) result end - month_map = { 'январь' => 1, 'февраль' => 2, @@ -196,7 +199,7 @@ def get_similar_price_products(data, products) 'сентябрь' => 9, 'октябрь' => 10, 'ноябрь' => 11, - 'декабрь' => 12, + 'декабрь' => 12 } regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] products = {} @@ -217,9 +220,13 @@ def get_similar_price_products(data, products) puts '' puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' min_price = get_min_price(products[key]) - puts 'Lowest was on '+min_price['year']+'/'+month_map[min_price['month']].to_s+' at price '+min_price['price'].to_s+' BYN' + puts 'Lowest was on ' + print min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' + print min_price['price'].to_s + ' BYN' max_price = get_max_price(products[key]) - puts 'Maximum was on '+max_price['year']+'/'+month_map[max_price['month']].to_s+' at price '+max_price['price'].to_s+' BYN' + puts 'Maximum was on ' + print max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' + print max_price['price'].to_s + ' BYN' similar_products = get_similar_price_products(recent_price_data, products) if similar_products.empty? puts 'No products for similar price' From 98ab24071a04ee824b335e61b35bfc90f2017b71 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 16:45:15 +0300 Subject: [PATCH 09/22] 7 fix7 --- Hryvicki Kirill/0/loadFiles.rb | 23 +++++++-------- Hryvicki Kirill/0/run.rb | 51 ++++++++++++++++------------------ 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Hryvicki Kirill/0/loadFiles.rb b/Hryvicki Kirill/0/loadFiles.rb index 2691542..f19d90c 100644 --- a/Hryvicki Kirill/0/loadFiles.rb +++ b/Hryvicki Kirill/0/loadFiles.rb @@ -1,18 +1,19 @@ require 'mechanize' require 'open-uri' require 'uri' +require 'webrick' agent = Mechanize.new page = agent.get('http://www.belstat.gov.by/ofitsialnaya-statistika/makroekonomika-i-okruzhayushchaya-sreda/tseny/operativnaya-informatsiya_4/srednie-tseny-na-potrebitelskie-tovary-i-uslugi-po-respublike-belarus') page.links_with(:href => /.xls/).each do |link| - str_link = link.href.to_s - str_link = URI.unescape(str_link) - str_link = URI.escape(str_link) - if /http:\/\/www.belstat.gov.by/ === str_link - f_link = str_link - else - f_link = 'http://www.belstat.gov.by' + str_link - end - download = open(f_link) - IO.copy_stream(download, "./data/#{download.base_uri.to_s.split('/')[-1]}") -end \ No newline at end of file + str_link = link.href.to_s + str_link = WEBrick::HTTPUtils.unescape(str_link) + str_link = WEBrick::HTTPUtils.escape(str_link) + f_link = if %r{http:\/\/www.belstat.gov.by}.match?(str_link) + str_link + else + 'http://www.belstat.gov.by' + str_link + end + download = URI.open(f_link) + IO.copy_stream(download, "./data/#{download.base_uri.to_s.split('/')[-1]}") +end diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 5349762..c0309c8 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -5,7 +5,7 @@ def get_file_instance(file_path) ext = file_path.split('.')[2] file_instance = nil if ext == 'xls' || ext == 'xlsx' - file_instance = Roo::Spreadsheet.open(file_path) + Roo::Spreadsheet.open(file_path) file_instance = conditiona_load(ext, file_path) else puts 'unsupported file type: ' + file_path @@ -43,22 +43,22 @@ def fetch_products_data(file_instance, products, regions) def add_product(products, key, year, month, regions, file_instance, n) products[key] = {} unless products[key] - products[key][year] = {} unless products[key][year] - products[key][year][month] = { - regions[0] => format_value(file_instance.cell('G', n), year), - regions[1] => format_value(file_instance.cell('I', n), year), - regions[2] => format_value(file_instance.cell('K', n), year), - regions[3] => format_value(file_instance.cell('M', n), year), - regions[4] => format_value(file_instance.cell('O', n), year), - regions[5] => format_value(file_instance.cell('Q', n), year), - regions[6] => format_value(file_instance.cell('S', n), year) - } + products[key][year] = {} unless products[key][year] + products[key][year][month] = { + regions[0] => format_value(file_instance.cell('G', n), year), + regions[1] => format_value(file_instance.cell('I', n), year), + regions[2] => format_value(file_instance.cell('K', n), year), + regions[3] => format_value(file_instance.cell('M', n), year), + regions[4] => format_value(file_instance.cell('O', n), year), + regions[5] => format_value(file_instance.cell('Q', n), year), + regions[6] => format_value(file_instance.cell('S', n), year) + } end def format_value(val, year) if val result = val.to_f - result = result / 10_000 if year.to_i < 2017 + result /= 10_000 if year.to_i < 2017 return result.round(2) end end @@ -71,25 +71,24 @@ def get_recent_price_data(key, products, month_map) year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] month_key = get_closest_month(current_month, product_month_data, month_map) - return { 'price' => product_month_data[month_key]['Minsk'], 'year' => year_key, 'month' => month_key, 'product' => key } + return { 'price' => product_month_data[month_key]['Minsk'], 'year' => year_key, + 'month' => month_key, 'product' => key } end def get_closest_year(current_year, product_data) if product_data[current_year] - year_key = current_year + return year_key = current_year else - year_key = product_data.keys.max { |a, b| a.to_i <=> b.to_i } + return year_key = product_data.keys.max { |a, b| a.to_i <=> b.to_i } end - year_key end def get_closest_month(current_month, product_data, month_map) if product_data[current_month] - month_key = current_month + return month_key = current_month else - month_key = product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } + return month_key = product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } end - month_key end def get_min_price(hash) @@ -98,7 +97,7 @@ def get_min_price(hash) hash.each do |year, year_hash| min_month_data = find_min_month(year_hash) min_year_data = find_min_year(year, min_month_data, min_year_price) - min_year_price = min_year_data['price'] + min_year_price = min_year_data['price'] if min_year_data['price'] result = actual_data(result, min_year_data) end result @@ -133,7 +132,7 @@ def get_max_price(hash) hash.each do |year, year_hash| max_month_data = find_max_month(year_hash) max_year_data = find_max_year(year, max_month_data, max_year_price) - max_year_price = max_year_data['price'] + max_year_price = max_year_data['price'] if max_year_data['price'] result = actual_data(result, max_year_data) end result @@ -174,7 +173,7 @@ def get_similar_price_products(data, products) year = data['year'] month = data['month'] origin_product = data['product'] - return form_similar_products_array(products, price, year, month, origin_product) + form_similar_products_array(products, price, year, month, origin_product) end def form_similar_products_array(products, price, year, month, origin_product) @@ -206,7 +205,7 @@ def form_similar_products_array(products, price, year, month, origin_product) file_paths = Dir['./data/*'] file_paths.each do |file_path| file_instance = get_file_instance(file_path) - fetch_products_data(file_instance, products, regions) if file_instance + fetch_products_data(file_instance, products, regions) if file_instance end loop do puts 'What price are you looking for?' @@ -220,12 +219,10 @@ def form_similar_products_array(products, price, year, month, origin_product) puts '' puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' min_price = get_min_price(products[key]) - puts 'Lowest was on ' - print min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' + puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' print min_price['price'].to_s + ' BYN' max_price = get_max_price(products[key]) - puts 'Maximum was on ' - print max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' + puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' print max_price['price'].to_s + ' BYN' similar_products = get_similar_price_products(recent_price_data, products) if similar_products.empty? From e7812bc17994cea7f135d026b4a708e279aeacf7 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 17:15:52 +0300 Subject: [PATCH 10/22] 8 fix --- Hryvicki Kirill/0/loadFiles.rb | 8 ++++---- Hryvicki Kirill/0/run.rb | 26 ++++++++++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Hryvicki Kirill/0/loadFiles.rb b/Hryvicki Kirill/0/loadFiles.rb index f19d90c..f609b50 100644 --- a/Hryvicki Kirill/0/loadFiles.rb +++ b/Hryvicki Kirill/0/loadFiles.rb @@ -10,10 +10,10 @@ str_link = WEBrick::HTTPUtils.unescape(str_link) str_link = WEBrick::HTTPUtils.escape(str_link) f_link = if %r{http:\/\/www.belstat.gov.by}.match?(str_link) - str_link - else - 'http://www.belstat.gov.by' + str_link - end + str_link + else + 'http://www.belstat.gov.by' + str_link + end download = URI.open(f_link) IO.copy_stream(download, "./data/#{download.base_uri.to_s.split('/')[-1]}") end diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index c0309c8..8e820c8 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -71,24 +71,26 @@ def get_recent_price_data(key, products, month_map) year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] month_key = get_closest_month(current_month, product_month_data, month_map) - return { 'price' => product_month_data[month_key]['Minsk'], 'year' => year_key, - 'month' => month_key, 'product' => key } + form_recent_price_data(product_month_data[month_key]['Minsk'], year_key, month_key, key) +end + +def form_recent_price_data(price, year, month, product) + { + 'price' => price, + 'year' => year, + 'month' => month, + 'product' => product + } end def get_closest_year(current_year, product_data) - if product_data[current_year] - return year_key = current_year - else - return year_key = product_data.keys.max { |a, b| a.to_i <=> b.to_i } - end + current_year if product_data[current_year] + product_data.keys.max_by(&:to_i) unless product_data[current_year] end def get_closest_month(current_month, product_data, month_map) - if product_data[current_month] - return month_key = current_month - else - return month_key = product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } - end + current_month if product_data[current_month] + product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } unless product_data[current_month] end def get_min_price(hash) From aae184e31223dc7b6bc05148d14da7529d9f73d9 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 17:39:03 +0300 Subject: [PATCH 11/22] 9 fix --- Hryvicki Kirill/0/loadFiles.rb | 2 +- Hryvicki Kirill/0/run.rb | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Hryvicki Kirill/0/loadFiles.rb b/Hryvicki Kirill/0/loadFiles.rb index f609b50..d93d261 100644 --- a/Hryvicki Kirill/0/loadFiles.rb +++ b/Hryvicki Kirill/0/loadFiles.rb @@ -5,7 +5,7 @@ agent = Mechanize.new page = agent.get('http://www.belstat.gov.by/ofitsialnaya-statistika/makroekonomika-i-okruzhayushchaya-sreda/tseny/operativnaya-informatsiya_4/srednie-tseny-na-potrebitelskie-tovary-i-uslugi-po-respublike-belarus') -page.links_with(:href => /.xls/).each do |link| +page.links_with(href: /.xls/).each do |link| str_link = link.href.to_s str_link = WEBrick::HTTPUtils.unescape(str_link) str_link = WEBrick::HTTPUtils.escape(str_link) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 8e820c8..9b2fd61 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -66,7 +66,7 @@ def format_value(val, year) def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s current_month = Time.now.strftime('%m') - month_map.each { |month, month_number| current_month = month if month_number == current_month } + current_month = parse_month(month_map) product_year_data = products[key]; year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] @@ -75,7 +75,7 @@ def get_recent_price_data(key, products, month_map) end def form_recent_price_data(price, year, month, product) - { + { 'price' => price, 'year' => year, 'month' => month, @@ -83,6 +83,11 @@ def form_recent_price_data(price, year, month, product) } end +def parse_month(month_map) + month_map.each { |month, month_number| current_month = month if month_number == current_month } + current_month +end + def get_closest_year(current_year, product_data) current_year if product_data[current_year] product_data.keys.max_by(&:to_i) unless product_data[current_year] From a497d5fad09baed2484ecd5116d085d69708b094 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 17:41:25 +0300 Subject: [PATCH 12/22] 9-1 fix --- Hryvicki Kirill/0/run.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 9b2fd61..00b850a 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -65,7 +65,6 @@ def format_value(val, year) def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s - current_month = Time.now.strftime('%m') current_month = parse_month(month_map) product_year_data = products[key]; year_key = get_closest_year(current_year, product_year_data) @@ -84,6 +83,7 @@ def form_recent_price_data(price, year, month, product) end def parse_month(month_map) + current_month = Time.now.strftime('%m') month_map.each { |month, month_number| current_month = month if month_number == current_month } current_month end From bfa5dcb6678592e5dd0baab7b66f3f0164575f93 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 17:43:04 +0300 Subject: [PATCH 13/22] 9-2 fix --- Hryvicki Kirill/0/run.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 00b850a..4cba1dc 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -66,7 +66,7 @@ def format_value(val, year) def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s current_month = parse_month(month_map) - product_year_data = products[key]; + product_year_data = products[key] year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] month_key = get_closest_month(current_month, product_month_data, month_map) From a9709339351843821648e7d50a7695daf4b92830 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 19:05:36 +0300 Subject: [PATCH 14/22] 10 fix --- .../0/{loadFiles.rb => load_files.rb} | 0 Hryvicki Kirill/0/run.rb | 49 +++++++++---------- 2 files changed, 24 insertions(+), 25 deletions(-) rename Hryvicki Kirill/0/{loadFiles.rb => load_files.rb} (100%) diff --git a/Hryvicki Kirill/0/loadFiles.rb b/Hryvicki Kirill/0/load_files.rb similarity index 100% rename from Hryvicki Kirill/0/loadFiles.rb rename to Hryvicki Kirill/0/load_files.rb diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 4cba1dc..3da1e99 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -4,7 +4,7 @@ def get_file_instance(file_path) ext = file_path.split('.')[2] file_instance = nil - if ext == 'xls' || ext == 'xlsx' + if ext.include?('xls') Roo::Spreadsheet.open(file_path) file_instance = conditiona_load(ext, file_path) else @@ -25,42 +25,37 @@ def conditiona_load(ext, file_path) def find_keys(word, products) result = [] - products.keys.each { |key| - result.push(key) if key.include?(word) - } + products.keys.each { |key| result.push(key) if key.include?(word) } result end def fetch_products_data(file_instance, products, regions) for n in 9..file_instance.last_row - next if file_instance.cell('E', n) == nil + next if file_instance.cell('E', n).nil? + year = file_instance.cell('A', 3).split(' ')[2] month = file_instance.cell('A', 3).split(' ')[1] key = file_instance.cell('A', n).strip.downcase - add_product(products, key, year, month, regions, file_instance, n) + meta = [key, year, month] + add_product(products, meta, regions, file_instance, n) end end -def add_product(products, key, year, month, regions, file_instance, n) +def add_product(products, meta, regions, file_instance, num) + key = meta[0] + year = meta[1] + month = meta[2] products[key] = {} unless products[key] products[key][year] = {} unless products[key][year] - products[key][year][month] = { - regions[0] => format_value(file_instance.cell('G', n), year), - regions[1] => format_value(file_instance.cell('I', n), year), - regions[2] => format_value(file_instance.cell('K', n), year), - regions[3] => format_value(file_instance.cell('M', n), year), - regions[4] => format_value(file_instance.cell('O', n), year), - regions[5] => format_value(file_instance.cell('Q', n), year), - regions[6] => format_value(file_instance.cell('S', n), year) - } + products[key][year][month] = { regions[4] => format_value(file_instance.cell('O', num), year) } end def format_value(val, year) - if val - result = val.to_f - result /= 10_000 if year.to_i < 2017 - return result.round(2) - end + return if val.nil? + + result = val.to_f + result /= 10_000 if year.to_i < 2017 + result.round(2) end def get_recent_price_data(key, products, month_map) @@ -114,6 +109,7 @@ def find_min_month(year_hash, min_month_price = 9_999_999_999_999, min_month = n year_hash.each do |month, month_hash| price = month_hash['Minsk'] next unless price + if price < min_month_price min_month_price = price min_month = month @@ -149,6 +145,7 @@ def find_max_month(year_hash, max_month_price = 0, max_month = nil) year_hash.each do |month, month_hash| price = month_hash['Minsk'] next unless price + if price > max_month_price max_month_price = price max_month = month @@ -185,11 +182,13 @@ def get_similar_price_products(data, products) def form_similar_products_array(products, price, year, month, origin_product) result = [] - products.each { |product, product_data| + products.each do |product, product_data| next unless product_data[year] + next unless product_data[year][month] + result.push(product) if product_data[year][month]['Minsk'] == price && product != origin_product - } + end result end @@ -216,7 +215,7 @@ def form_similar_products_array(products, price, year, month, origin_product) end loop do puts 'What price are you looking for?' - word = gets.chomp.downcase.encode('UTF-8') + word = gets.chomp.downcase # .encode('UTF-8') keys = find_keys(word, products) if keys.empty? puts word.capitalize + ' can not be found in database' @@ -224,7 +223,7 @@ def form_similar_products_array(products, price, year, month, origin_product) keys.each do |key| recent_price_data = get_recent_price_data(key, products, month_map) puts '' - puts key.capitalize+' is '+recent_price_data['price'].to_s+' BYN in Minsk these days.' + puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' min_price = get_min_price(products[key]) puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' print min_price['price'].to_s + ' BYN' From 1dd7ba800632539ee5b925ec638808431c7d3b32 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 20:06:49 +0300 Subject: [PATCH 15/22] 11 fix --- Hryvicki Kirill/0/load_files.rb | 2 +- Hryvicki Kirill/0/run.rb | 109 ++++++++++++++++---------------- 2 files changed, 54 insertions(+), 57 deletions(-) diff --git a/Hryvicki Kirill/0/load_files.rb b/Hryvicki Kirill/0/load_files.rb index d93d261..00dc901 100644 --- a/Hryvicki Kirill/0/load_files.rb +++ b/Hryvicki Kirill/0/load_files.rb @@ -9,7 +9,7 @@ str_link = link.href.to_s str_link = WEBrick::HTTPUtils.unescape(str_link) str_link = WEBrick::HTTPUtils.escape(str_link) - f_link = if %r{http:\/\/www.belstat.gov.by}.match?(str_link) + f_link = if str_link.start_with?('/') str_link else 'http://www.belstat.gov.by' + str_link diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 3da1e99..9edcc3a 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -5,15 +5,14 @@ def get_file_instance(file_path) ext = file_path.split('.')[2] file_instance = nil if ext.include?('xls') - Roo::Spreadsheet.open(file_path) - file_instance = conditiona_load(ext, file_path) + file_instance = conditional_load(ext, file_path) else puts 'unsupported file type: ' + file_path end file_instance end -def conditiona_load(ext, file_path) +def conditional_load(ext, file_path) case ext when 'xls' sheet = Roo::Excel.new(file_path) @@ -23,14 +22,8 @@ def conditiona_load(ext, file_path) sheet end -def find_keys(word, products) - result = [] - products.keys.each { |key| result.push(key) if key.include?(word) } - result -end - def fetch_products_data(file_instance, products, regions) - for n in 9..file_instance.last_row + (9..file_instance.last_row).each do |n| next if file_instance.cell('E', n).nil? year = file_instance.cell('A', 3).split(' ')[2] @@ -94,7 +87,7 @@ def get_closest_month(current_month, product_data, month_map) end def get_min_price(hash) - min_year_price = 9_999_999_999_999 + min_year_price = Float::INFINITY result = {} hash.each do |year, year_hash| min_month_data = find_min_month(year_hash) @@ -105,7 +98,7 @@ def get_min_price(hash) result end -def find_min_month(year_hash, min_month_price = 9_999_999_999_999, min_month = nil) +def find_min_month(year_hash, min_month_price = Float::INFINITY, min_month = nil) year_hash.each do |month, month_hash| price = month_hash['Minsk'] next unless price @@ -192,51 +185,55 @@ def form_similar_products_array(products, price, year, month, origin_product) result end -month_map = { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12 -} -regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] -products = {} -file_paths = Dir['./data/*'] -file_paths.each do |file_path| - file_instance = get_file_instance(file_path) - fetch_products_data(file_instance, products, regions) if file_instance -end -loop do - puts 'What price are you looking for?' - word = gets.chomp.downcase # .encode('UTF-8') - keys = find_keys(word, products) - if keys.empty? - puts word.capitalize + ' can not be found in database' - else - keys.each do |key| - recent_price_data = get_recent_price_data(key, products, month_map) - puts '' - puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' - min_price = get_min_price(products[key]) - puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' - print min_price['price'].to_s + ' BYN' - max_price = get_max_price(products[key]) - puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' - print max_price['price'].to_s + ' BYN' - similar_products = get_similar_price_products(recent_price_data, products) - if similar_products.empty? - puts 'No products for similar price' - else - puts 'For similar price you also can afford' - puts similar_products +def main() + month_map = { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12 + } + regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + products = {} + file_paths = Dir['./data/*'] + file_paths.each do |file_path| + file_instance = get_file_instance(file_path) + fetch_products_data(file_instance, products, regions) if file_instance + end + loop do + puts 'What price are you looking for?' + word = gets.chomp.downcase.encode('UTF-8') + keys = products.keys.select { |key| key.include?(word) } + if keys.empty? + puts word.capitalize + ' can not be found in database' + else + keys.each do |key| + recent_price_data = get_recent_price_data(key, products, month_map) + puts '' + puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' + min_price = get_min_price(products[key]) + puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' + print min_price['price'].to_s + ' BYN' + max_price = get_max_price(products[key]) + puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' + print max_price['price'].to_s + ' BYN' + similar_products = get_similar_price_products(recent_price_data, products) + if similar_products.empty? + puts 'No products for similar price' + else + puts 'For similar price you also can afford' + puts similar_products + end end end end end + +main From 448c419d73d44ff8ff33495e420464df401de04d Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 20:32:28 +0300 Subject: [PATCH 16/22] 12 fix --- Hryvicki Kirill/0/run.rb | 100 +++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 40 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 9edcc3a..2e6806a 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -22,7 +22,7 @@ def conditional_load(ext, file_path) sheet end -def fetch_products_data(file_instance, products, regions) +def fetch_products_data(file_instance) (9..file_instance.last_row).each do |n| next if file_instance.cell('E', n).nil? @@ -30,17 +30,17 @@ def fetch_products_data(file_instance, products, regions) month = file_instance.cell('A', 3).split(' ')[1] key = file_instance.cell('A', n).strip.downcase meta = [key, year, month] - add_product(products, meta, regions, file_instance, n) + add_product(meta, file_instance, n) end end -def add_product(products, meta, regions, file_instance, num) +def add_product(meta, file_instance, num) key = meta[0] year = meta[1] month = meta[2] - products[key] = {} unless products[key] - products[key][year] = {} unless products[key][year] - products[key][year][month] = { regions[4] => format_value(file_instance.cell('O', num), year) } + $products[key] = {} unless $products[key] + $products[key][year] = {} unless $products[key][year] + $products[key][year][month] = { $regions[4] => format_value(file_instance.cell('O', num), year) } end def format_value(val, year) @@ -51,13 +51,13 @@ def format_value(val, year) result.round(2) end -def get_recent_price_data(key, products, month_map) +def get_recent_price_data(key) current_year = Time.now.strftime('%Y').to_s - current_month = parse_month(month_map) - product_year_data = products[key] + current_month = parse_month + product_year_data = $products[key] year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] - month_key = get_closest_month(current_month, product_month_data, month_map) + month_key = get_closest_month(current_month, product_month_data) form_recent_price_data(product_month_data[month_key]['Minsk'], year_key, month_key, key) end @@ -70,9 +70,9 @@ def form_recent_price_data(price, year, month, product) } end -def parse_month(month_map) +def parse_month current_month = Time.now.strftime('%m') - month_map.each { |month, month_number| current_month = month if month_number == current_month } + $month_map.each { |month, month_number| current_month = month if month_number == current_month } current_month end @@ -81,9 +81,9 @@ def get_closest_year(current_year, product_data) product_data.keys.max_by(&:to_i) unless product_data[current_year] end -def get_closest_month(current_month, product_data, month_map) +def get_closest_month(current_month, product_data) current_month if product_data[current_month] - product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } unless product_data[current_month] + product_data.keys.max { |a, b| $month_map[a].to_i <=> $month_map[b].to_i } unless product_data[current_month] end def get_min_price(hash) @@ -165,17 +165,17 @@ def actual_data(result, year_data) result end -def get_similar_price_products(data, products) +def get_similar_price_products(data) price = data['price'] year = data['year'] month = data['month'] origin_product = data['product'] - form_similar_products_array(products, price, year, month, origin_product) + form_similar_products_array( price, year, month, origin_product) end -def form_similar_products_array(products, price, year, month, origin_product) +def form_similar_products_array( price, year, month, origin_product) result = [] - products.each do |product, product_data| + $products.each do |product, product_data| next unless product_data[year] next unless product_data[year][month] @@ -185,8 +185,8 @@ def form_similar_products_array(products, price, year, month, origin_product) result end -def main() - month_map = { +def init_variables + $month_map = { 'январь' => 1, 'февраль' => 2, 'март' => 3, @@ -200,37 +200,57 @@ def main() 'ноябрь' => 11, 'декабрь' => 12 } - regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - products = {} + $regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + $products = {} file_paths = Dir['./data/*'] file_paths.each do |file_path| file_instance = get_file_instance(file_path) - fetch_products_data(file_instance, products, regions) if file_instance + fetch_products_data(file_instance) if file_instance end +end + +def first_level(key, recent_price_data) + puts '' + puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' +end + +def second_level_min_price(key) + min_price = get_min_price($products[key]) + puts 'Lowest was on ' + min_price['year'] + '/' + $month_map[min_price['month']].to_s + ' at price ' + print min_price['price'].to_s + ' BYN' +end + +def second_level_max_price(key) + max_price = get_max_price($products[key]) + puts 'Maximum was on ' + max_price['year'] + '/' + $month_map[max_price['month']].to_s + ' at price ' + print max_price['price'].to_s + ' BYN' +end + +def third_level(recent_price_data) + similar_products = get_similar_price_products(recent_price_data) + if similar_products.empty? + puts 'No products for similar price' + else + puts 'For similar price you also can afford' + puts similar_products + end +end + +def main + init_variables loop do puts 'What price are you looking for?' word = gets.chomp.downcase.encode('UTF-8') - keys = products.keys.select { |key| key.include?(word) } + keys = $products.keys.select { |key| key.include?(word) } if keys.empty? puts word.capitalize + ' can not be found in database' else keys.each do |key| - recent_price_data = get_recent_price_data(key, products, month_map) - puts '' - puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' - min_price = get_min_price(products[key]) - puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' - print min_price['price'].to_s + ' BYN' - max_price = get_max_price(products[key]) - puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' - print max_price['price'].to_s + ' BYN' - similar_products = get_similar_price_products(recent_price_data, products) - if similar_products.empty? - puts 'No products for similar price' - else - puts 'For similar price you also can afford' - puts similar_products - end + recent_price_data = get_recent_price_data(key) + first_level(key, recent_price_data) + second_level_min_price(key) + second_level_max_price(key) + third_level(recent_price_data) end end end From ad5d76d77801d7a8a5a863398079de18e2a66f85 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 21:57:43 +0300 Subject: [PATCH 17/22] 13 fix --- Hryvicki Kirill/0/run.rb | 138 +++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 65 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 2e6806a..0ec7c79 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -22,7 +22,7 @@ def conditional_load(ext, file_path) sheet end -def fetch_products_data(file_instance) +def fetch_products_data(file_instance, products, regions) (9..file_instance.last_row).each do |n| next if file_instance.cell('E', n).nil? @@ -30,17 +30,17 @@ def fetch_products_data(file_instance) month = file_instance.cell('A', 3).split(' ')[1] key = file_instance.cell('A', n).strip.downcase meta = [key, year, month] - add_product(meta, file_instance, n) + add_product(products, meta, regions, file_instance, n) end end -def add_product(meta, file_instance, num) +def add_product(products, meta, regions, file_instance, num) key = meta[0] year = meta[1] month = meta[2] - $products[key] = {} unless $products[key] - $products[key][year] = {} unless $products[key][year] - $products[key][year][month] = { $regions[4] => format_value(file_instance.cell('O', num), year) } + products[key] = {} unless products[key] + products[key][year] = {} unless products[key][year] + products[key][year][month] = { regions[4] => format_value(file_instance.cell('O', num), year) } end def format_value(val, year) @@ -51,13 +51,13 @@ def format_value(val, year) result.round(2) end -def get_recent_price_data(key) +def get_recent_price_data(key, products, month_map) current_year = Time.now.strftime('%Y').to_s - current_month = parse_month - product_year_data = $products[key] + current_month = parse_month(month_map) + product_year_data = products[key] year_key = get_closest_year(current_year, product_year_data) product_month_data = product_year_data[year_key] - month_key = get_closest_month(current_month, product_month_data) + month_key = get_closest_month(current_month, product_month_data, month_map) form_recent_price_data(product_month_data[month_key]['Minsk'], year_key, month_key, key) end @@ -70,9 +70,9 @@ def form_recent_price_data(price, year, month, product) } end -def parse_month +def parse_month(month_map) current_month = Time.now.strftime('%m') - $month_map.each { |month, month_number| current_month = month if month_number == current_month } + month_map.each { |month, month_number| current_month = month if month_number == current_month } current_month end @@ -81,9 +81,9 @@ def get_closest_year(current_year, product_data) product_data.keys.max_by(&:to_i) unless product_data[current_year] end -def get_closest_month(current_month, product_data) +def get_closest_month(current_month, product_data, month_map) current_month if product_data[current_month] - product_data.keys.max { |a, b| $month_map[a].to_i <=> $month_map[b].to_i } unless product_data[current_month] + product_data.keys.max { |a, b| month_map[a].to_i <=> month_map[b].to_i } unless product_data[current_month] end def get_min_price(hash) @@ -165,17 +165,17 @@ def actual_data(result, year_data) result end -def get_similar_price_products(data) +def get_similar_price_products(data, products) price = data['price'] year = data['year'] month = data['month'] origin_product = data['product'] - form_similar_products_array( price, year, month, origin_product) + form_similar_products_array(products, price, year, month, origin_product) end -def form_similar_products_array( price, year, month, origin_product) +def form_similar_products_array(products, price, year, month, origin_product) result = [] - $products.each do |product, product_data| + products.each do |product, product_data| next unless product_data[year] next unless product_data[year][month] @@ -185,49 +185,25 @@ def form_similar_products_array( price, year, month, origin_product) result end -def init_variables - $month_map = { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12 - } - $regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] - $products = {} - file_paths = Dir['./data/*'] - file_paths.each do |file_path| - file_instance = get_file_instance(file_path) - fetch_products_data(file_instance) if file_instance - end -end - def first_level(key, recent_price_data) puts '' puts key.capitalize + ' is ' + recent_price_data['price'].to_s + ' BYN in Minsk these days.' end -def second_level_min_price(key) - min_price = get_min_price($products[key]) - puts 'Lowest was on ' + min_price['year'] + '/' + $month_map[min_price['month']].to_s + ' at price ' +def second_level_min_price(product_data, month_map) + min_price = get_min_price(product_data) + puts 'Lowest was on ' + min_price['year'] + '/' + month_map[min_price['month']].to_s + ' at price ' print min_price['price'].to_s + ' BYN' end -def second_level_max_price(key) - max_price = get_max_price($products[key]) - puts 'Maximum was on ' + max_price['year'] + '/' + $month_map[max_price['month']].to_s + ' at price ' +def second_level_max_price(product_data, month_map) + max_price = get_max_price(product_data) + puts 'Maximum was on ' + max_price['year'] + '/' + month_map[max_price['month']].to_s + ' at price ' print max_price['price'].to_s + ' BYN' end -def third_level(recent_price_data) - similar_products = get_similar_price_products(recent_price_data) +def third_level(recent_price_data, products) + similar_products = get_similar_price_products(recent_price_data, products) if similar_products.empty? puts 'No products for similar price' else @@ -236,23 +212,55 @@ def third_level(recent_price_data) end end +def make_products_container + regions = ['Brest', 'Vitebsk', 'Gomel', 'Grodno', 'Minsk', 'Minsk Region', 'Mogilyov'] + products = {} + file_paths = Dir['./data/*'] + file_paths.each do |file_path| + file_instance = get_file_instance(file_path) + fetch_products_data(file_instance, products, regions) if file_instance + end + products +end + +def user_wish(products) + puts 'What price are you looking for?' + word = gets.chomp.downcase.encode('UTF-8') + products.keys.select { |key| key.include?(word) } +end + +def month_map_former + { + 'январь' => 1, + 'февраль' => 2, + 'март' => 3, + 'апрель' => 4, + 'май' => 5, + 'июнь' => 6, + 'июль' => 7, + 'авуст' => 8, + 'сентябрь' => 9, + 'октябрь' => 10, + 'ноябрь' => 11, + 'декабрь' => 12 + } +end + +def process_request(products, key, month_map) + recent_price_data = get_recent_price_data(key, products, month_map) + first_level(key, recent_price_data) + second_level_min_price(products[key], month_map) + second_level_max_price(products[key], month_map) + third_level(recent_price_data, products) +end + def main - init_variables + month_map = month_map_former + products = make_products_container loop do - puts 'What price are you looking for?' - word = gets.chomp.downcase.encode('UTF-8') - keys = $products.keys.select { |key| key.include?(word) } - if keys.empty? - puts word.capitalize + ' can not be found in database' - else - keys.each do |key| - recent_price_data = get_recent_price_data(key) - first_level(key, recent_price_data) - second_level_min_price(key) - second_level_max_price(key) - third_level(recent_price_data) - end - end + keys = user_wish(products) + puts word.capitalize + ' can not be found in database' if keys.empty? + keys.each { |key| process_request(products, key, month_map) } unless keys.empty? end end From c477f857a062dde62134fa603da4e623d620553a Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 22:02:00 +0300 Subject: [PATCH 18/22] 13-1 fix --- Hryvicki Kirill/0/run.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 0ec7c79..80b266b 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -231,18 +231,12 @@ def user_wish(products) def month_map_former { - 'январь' => 1, - 'февраль' => 2, - 'март' => 3, - 'апрель' => 4, - 'май' => 5, - 'июнь' => 6, - 'июль' => 7, - 'авуст' => 8, - 'сентябрь' => 9, - 'октябрь' => 10, - 'ноябрь' => 11, - 'декабрь' => 12 + 'январь' => 1, 'февраль' => 2, + 'март' => 3, 'апрель' => 4, + 'май' => 5, 'июнь' => 6, + 'июль' => 7, 'авуст' => 8, + 'сентябрь' => 9, 'октябрь' => 10, + 'ноябрь' => 11, 'декабрь' => 12 } end From 2d513056d8d22e62abb050bdbc2347a28354fb28 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 22:35:00 +0300 Subject: [PATCH 19/22] gemfile + readme --- Hryvicki Kirill/0/Gemfile | 5 +++++ Hryvicki Kirill/0/README.md | 9 +++++++++ 2 files changed, 14 insertions(+) create mode 100644 Hryvicki Kirill/0/Gemfile create mode 100644 Hryvicki Kirill/0/README.md diff --git a/Hryvicki Kirill/0/Gemfile b/Hryvicki Kirill/0/Gemfile new file mode 100644 index 0000000..1b36532 --- /dev/null +++ b/Hryvicki Kirill/0/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem 'mechanize' +gem 'roo' +gem 'roo-xls' diff --git a/Hryvicki Kirill/0/README.md b/Hryvicki Kirill/0/README.md new file mode 100644 index 0000000..a61bba6 --- /dev/null +++ b/Hryvicki Kirill/0/README.md @@ -0,0 +1,9 @@ +# Usage + +bundle install + +create empty data folder + +ruby load_files.rb + +ruby run.rb \ No newline at end of file From 8fecc372f03b6f49584b82b786caa7a35885a2fa Mon Sep 17 00:00:00 2001 From: ypsl <43265168+ypsl@users.noreply.github.com> Date: Sun, 9 Dec 2018 22:38:29 +0300 Subject: [PATCH 20/22] Update README.md --- Hryvicki Kirill/0/README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Hryvicki Kirill/0/README.md b/Hryvicki Kirill/0/README.md index a61bba6..98b66bc 100644 --- a/Hryvicki Kirill/0/README.md +++ b/Hryvicki Kirill/0/README.md @@ -1,9 +1,11 @@ # Usage - +``` bundle install - -create empty data folder - +``` +``` ruby load_files.rb - -ruby run.rb \ No newline at end of file +``` +``` +ruby run.rb +``` +before loading files create empty folder named as 'data' From 5c964c4511be75c622c456b3f774e2c3e819f202 Mon Sep 17 00:00:00 2001 From: ypsl <43265168+ypsl@users.noreply.github.com> Date: Sun, 9 Dec 2018 22:38:55 +0300 Subject: [PATCH 21/22] Update Gemfile --- Hryvicki Kirill/0/Gemfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Hryvicki Kirill/0/Gemfile b/Hryvicki Kirill/0/Gemfile index 1b36532..4cb85ab 100644 --- a/Hryvicki Kirill/0/Gemfile +++ b/Hryvicki Kirill/0/Gemfile @@ -1,5 +1,5 @@ -source "https://rubygems.org" +source 'https://rubygems.org' gem 'mechanize' -gem 'roo' +gem 'roo' gem 'roo-xls' From 38add5b0e50bafea65c74365e89c04e8e14b23c5 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 9 Dec 2018 22:49:35 +0300 Subject: [PATCH 22/22] 13-3 parse product method added --- Hryvicki Kirill/0/run.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Hryvicki Kirill/0/run.rb b/Hryvicki Kirill/0/run.rb index 80b266b..006a19a 100644 --- a/Hryvicki Kirill/0/run.rb +++ b/Hryvicki Kirill/0/run.rb @@ -38,9 +38,13 @@ def add_product(products, meta, regions, file_instance, num) key = meta[0] year = meta[1] month = meta[2] + parse_product(products, key, year) + products[key][year][month] = { regions[4] => format_value(file_instance.cell('O', num), year) } +end + +def parse_product(products, key, year) products[key] = {} unless products[key] products[key][year] = {} unless products[key][year] - products[key][year][month] = { regions[4] => format_value(file_instance.cell('O', num), year) } end def format_value(val, year)