Coder Social home page Coder Social logo

challenge2016's Introduction

Real Image Challenge 2016

In the cinema business, a feature film is usually provided to a regional distributor based on a contract for exhibition in a particular geographical territory.

Each authorization is specified by a combination of included and excluded regions. For example, a distributor might be authorzied in the following manner:

Permissions for DISTRIBUTOR1
INCLUDE: INDIA
INCLUDE: UNITEDSTATES
EXCLUDE: KARNATAKA-INDIA
EXCLUDE: CHENNAI-TAMILNADU-INDIA

This allows DISTRIBUTOR1 to distribute in any city inside the United States and India, except cities in the state of Karnataka (in India) and the city of Chennai (in Tamil Nadu, India).

At this point, asking your program if DISTRIBUTOR1 has permission to distribute in CHICAGO-ILLINOIS-UNITEDSTATES should get YES as the answer, and asking if distribution can happen in CHENNAI-TAMILNADU-INDIA should of course be NO. Asking if distribution is possible in BANGALORE-KARNATAKA-INDIA should also be NO, because the whole state of Karnataka has been excluded.

Sometimes, a distributor might split the work of distribution amount smaller sub-distiributors inside their authorized geographies. For instance, DISTRIBUTOR1 might assign the following permissions to DISTRIBUTOR2:

Permissions for DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: INDIA
EXCLUDE: TAMILNADU-INDIA

Now, DISTRIBUTOR2 can distribute the movie anywhere in INDIA, except inside TAMILNADU-INDIA and KARNATAKA-INDIA - DISTRIBUTOR2's permissions are always a subset of DISTRIBUTOR1's permissions. It's impossible/invalid for DISTRIBUTOR2 to have INCLUDE: CHINA, for example, because DISTRIBUTOR1 isn't authorized to do that in the first place.

If DISTRIBUTOR2 authorizes DISTRIBUTOR3 to handle just the city of Hubli, Karnataka, India, for example:

Permissions for DISTRIBUTOR3 < DISTRIBUTOR2 < DISTRIBUTOR1
INCLUDE: HUBLI-KARNATAKA-INDIA

Again, DISTRIBUTOR2 cannot authorize DISTRIBUTOR3 with a region that they themselves do not have access to.

We've provided a CSV with the list of all countries, states and cities in the world that we know of - please use the data mentioned there for this program. The codes you see there may be different from what you see here, so please always use the codes in the CSV. This Readme is only an example.

Write a program in any language you want (If you're here from Gophercon, use Go :D) that does this. Feel free to make your own input and output format / command line tool / GUI / Webservice / whatever you want. Feel free to hold the dataset in whatever structure you want, but try not to use external databases - as far as possible stick to your langauage without bringing in MySQL/Postgres/MongoDB/Redis/Etc.

To submit a solution, fork this repo and send a Pull Request on Github.

For any questions or clarifications, raise an issue on this repo and we'll answer your questions as fast as we can.

challenge2016's People

Contributors

sivachandran avatar sudhirj avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

challenge2016's Issues

solution for ruby

class Qube

def self.add_distributor_name
@DaTa = Hash.new {|h, k| h[k] = []}
add_distributor
end

def self.add_distributor
puts "Enter 0 if you want exit program. \nEnter 1 if you want add distributor. \nEnter 2 if you want add distribution permission on a particular distributor"
status = gets.chomp.to_i
case status
when 1
puts 'Please enter name of the distributor.'
distributor_name = gets.chomp.to_s.downcase
if @data.key?(distributor_name)
puts "#{distributor_name} already present"
else
puts "#{distributor_name} added successfully"
@DaTa[distributor_name] = {"include" => [], "exclude" => []}
end
add_distributor
when 2
add_area('');
when 0
else
puts 'please enter a valid input'
add_distributor
end
end

def self.add_area(current_distributor)
if @data.length === 0
puts 'Please add at least one distributor'
add_distributor
else
if current_distributor && @data.key?(current_distributor) && @data.length > 1
puts 'If you want change distributor please enter 1 else enter'
if gets.chomp.to_i === 1
add_area('')
else
distributor_name = current_distributor
end
elsif @data.length === 1
distributor_name = @data.keys[0]
else
puts 'Please select distributor for adding permission'
puts 'Given below available distributes list'
@data.each_pair {|key, value| puts "#{key}"}
distributor_name = gets.chomp.to_s.downcase
end
if @data.key?(distributor_name)
puts "select 1 for include permission,select 2 for exclude permission. If you don't want add permission please enter 0"
permission = gets.chomp.to_i
puts 'Please make sure if you want include or exclude entire country, Please enter country name ex:india or India'
puts "If you want include or exclude Province, please enter Province Name and Country Name with comma separation\n ex:Jammu and Kashmir,India"
puts "If you want include or exclude City Please enter City Name,Province Name,Country Name with comma separation\n ex:Punch,Jammu and Kashmir,India"
if permission == 1
add_areas('include', distributor_name, 'exclude')
elsif permission == 2
add_areas('exclude', distributor_name, 'include')
else
puts "Want to add more areas for distribution right please select 1"
puts "Want to check particular distributor have present permission on some area please select 2 "
puts "Want to selling distribution rights please select 3"
puts "Want to exit program please select 0"
status = gets.chomp.to_i
if status === 1
add_area(distributor_name)
elsif status === 2
check_right
elsif status === 3
parent_add
elsif status === 0
else
puts "you entered wrong value"
add_area(current_distributor)
end
end
else
puts "please select valid distributor"
add_area(distributor_name)
end
end
end

def self.add_areas(type, distributor_name, opp_type)
puts "Please enter which area you want to #{type} for #{distributor_name}"
area = gets.chomp.to_s.downcase
split_area = area.split(',')
if @DaTa[distributor_name][opp_type].include?(area) || @DaTa[distributor_name][type].include?(area)
puts "Please check #{area} that already present in #{opp_type} or #{type}"
add_area(distributor_name)
else
if split_area.length === 2 && @DaTa[distributor_name]["#{type}"].include?(split_area[1])
puts "Already you have present permission"
elsif split_area.length == 3 && (@DaTa[distributor_name]["#{type}"].include?(split_area[1]) || @DaTa[distributor_name]["#{type}"].include?("#{split_area[1]},#{split_area[2]}"))
puts "Already you have present permission"
else
if check_data_valid(area)
if split_area.length > 1 && type == 'exclude' && @DaTa[distributor_name]["include"].length > 0
if (split_area.length == 2) && (@DaTa[distributor_name]["include"].include?(split_area[1]) || @DaTa[distributor_name]["include"].include?("#{split_area[0]},#{split_area[1]}"))
@DaTa[distributor_name]["#{type}"].push(area)
elsif (split_area.length == 3) && (@DaTa[distributor_name]["include"].include?("#{split_area[1]},#{split_area[2]}") || @DaTa[distributor_name]["include"].include?(split_area[2]))
@DaTa[distributor_name]["#{type}"].push(area)
else
puts "You don't have permission for distribution for #{split_area[1]}"
end
elsif type == 'include'
@DaTa[distributor_name]["#{type}"].push(area)
else
puts "please first add distribution permission in include section"
end
if split_area.length > 0
@DaTa[distributor_name]["#{type}"].each do |data|
if (data =~ /^\w+,#{area}/)
@DaTa[distributor_name]["#{type}"].delete(data)
end
end
end
else
puts "Please check #{area} that not available"
end
end
end
add_area(distributor_name)
end

def self.parent_add
if @data.length > 1
puts "if you want selling your distribution right to someone please enter yes"
if gets.chomp.to_s.downcase == 'yes'
puts "please select seller name\n Given below showing available distributes list"
@data.each_pair {|key, value| puts "#{key}"}
seller = gets.chomp.to_s.downcase
if @data.key?(seller) && (!@DaTa[seller].key?("include") || @DaTa[seller]["include"].length === 0)
puts "#{seller} don't have any distribution rights "
parent_add
elsif [email protected]?(seller)
puts "#{seller} not present"
parent_add
else
puts "please select buyer name"
buyer = gets.chomp.to_s.downcase
if @data.key?(seller) && @data.key?(buyer) && seller != buyer
puts "select which area distribution you want sell"
area = gets.chomp.to_s.downcase
area_list = area.split(',')
if (area_list.length === 1) && @DaTa[seller]["include"].include?(area)
@DaTa[buyer]["include"].push(area)
@DaTa[seller]["include"].delete(area)
@DaTa[buyer]["exclude"].delete(area)
@DaTa[seller]["exclude"].delete(area)
puts "Successfully transfer your #{area} right to #{buyer}"
elsif area_list.length === 2 && !@DaTa[seller]["exclude"].include?(area) && !@DaTa[seller]["exclude"].include?(area_list[1])
if @DaTa[seller]["include"].include?(area) || @DaTa[seller]["include"].include?(area_list[1])
@DaTa[buyer]["include"].push(area)
@DaTa[seller]["include"].delete(area)
@DaTa[seller]["exclude"].push(area)
@DaTa[buyer]["exclude"].delete(area)
puts "Successfully transfer your #{area} right to #{buyer}"
else
puts "you can't the right for selling #{area}"
end
elsif (area_list.length === 3) && !@DaTa[seller]["exclude"].include?(area) && !@DaTa[seller]["exclude"].include?("#{area_list[1]},#{area_list[2]}")
if (@DaTa[seller]["include"].include?(area) || @DaTa[seller]["include"].include?(area_list[2])) || @DaTa[seller]["include"].include?("#{area_list[1]},#{area_list[2]}")
@DaTa[buyer]["include"].push(area)
@DaTa[seller]["exclude"].push(area)
puts "Successfully transfer your #{area} right to #{buyer}"
else
puts "you can't the right for selling #{area}"
end
else
puts "you can't the right for selling #{area}"
end
parent_add
else
puts "Invalid distributor"
parent_add
end
end
else
puts "Want to add new distributor please select 1 "
puts "Want to check distribution rights for particular distributor please select 2 "
puts "Want to selling distribution right please select 3 "
puts "Want to exit program select 0"
status = gets.chomp.to_i
if status === 1
add_distributor
elsif status === 2
check_right
elsif status === 0
elsif status === 3
parent_add
else
puts "you entered wrong data"
parent_add
end
end
else
puts "Please add at least one more distributor"
puts "Want to add new distributor please select 1 "
puts "Want to check area permission select 2 "
puts "Want to exit select 0"
status = gets.chomp.to_i
if status === 1
add_distributor
elsif status === 2
check_right
elsif status === 0
else
puts "you entered wrong data"
parent_add
end
end
end

def self.check_right
if @data.length === 1
distributor = @data.keys[0]
else
puts "Please select distributor name"
@data.each_pair {|key, value| puts "#{key}"}
distributor = gets.chomp.to_s.downcase
end
if @data.key?(distributor) && (!@DaTa[distributor].key?("include") || @DaTa[distributor]["include"].length === 0)
puts "#{distributor} don't have any distribution right "
check_right
elsif [email protected]?(distributor)
puts "#{distributor} not present"
check_right
else
puts "Please enter area which you want check"
area = gets.chomp.to_s.downcase
area_list = area.split(',')
if @DaTa[distributor]["exclude"].include?(area)
puts "No....."
elsif @DaTa[distributor]["include"].include?(area)
puts "yes....."
else
if check_data_valid(area) && area_list.length === 2 && !@DaTa[distributor]["exclude"].include?(area_list[1]) && @DaTa[distributor]["include"].include?(area_list[1])
puts "yes....."
elsif check_data_valid(area) && area_list.length === 3 && !@DaTa[distributor]["exclude"].include?(area_list[2]) && (@DaTa[distributor]["include"].include?("#{area_list[1]},#{area_list[2]}") || @DaTa[distributor]["include"].include?(area_list[2]))
puts "yes....."
else
puts "no"
end
end
end
check_right
end

def self.check_data_valid(area)
f = File.open("cites.csv", "r")
data = area.split(',')
if data.length === 1
f.drop(1).each do |line|
x = line.split(",")
if x[5].chomp.downcase === data[0].downcase
return true
break
end
end
return false
elsif data.length === 2
f.each do |line|
x = line.split(",")
if x[4].chomp.downcase === data[0].downcase && x[5].chomp.downcase === data[1].downcase
return true
break
end
end
return false
elsif data.length === 3
f.each do |line|
x = line.split(",")
if x[3].chomp.downcase === data[0].downcase && x[4].chomp.downcase === data[1].downcase && x[5].chomp.downcase === data[2].downcase
return true
break
end
end
return false
else
return false
end
end
end

Qube.add_distributor_name

Using ruby

check_data_valid method please add correct cites.csv file path

here i am using logic for selling distribution is, if one distributor sell his distribution to someone first seller exclude that corresponding rights and buyer including that corresponding rights

Doubts

doubt 1:
Are the values of INCLUDE and EXCLUDE maintain same order which is 'CITY-PROVINCE-COUNTRY' ?

doubt 2:
I'm assuming I should be providing a way to add authorizations for a distributor and also accept input to evaluate with the given authorization and output YES or NO. Is it?
I'll Make the app accept a file containing authorizations so that dont have to input text again and again everytime the app is run. Hope that's okay.

doubt 3:
will the values of INCLUDE and EXCLUDE be city id, province id and country id from csv or the names. I'm assuming those would be codes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.