Skip to content

Instantly share code, notes, and snippets.

@JuliusNM
Created September 30, 2016 03:12
Show Gist options
  • Save JuliusNM/36f4df57cacb9d3cccb471f0d0dd2b35 to your computer and use it in GitHub Desktop.
Save JuliusNM/36f4df57cacb9d3cccb471f0d0dd2b35 to your computer and use it in GitHub Desktop.
def product_array(arr)
prd=1
arr.each do |x|
x > 0 ? prd= prd*x : prd = prd
end
prd
end
def new_array (arr)
zero_count = arr.count(0)
arry = []
if zero_count == 1
prd = product_array (arr)
arr.each {|i| i == 0 ? arry.push(prd) : arry.push(0) }
elsif zero_count > 1
arr.each {|i| arry.push(0)}
else
y = product_array (arr)
arr.each do |x|
arry.push(y/x)
end
end
arry
end
a = [1,2,0,2]
ans = new_array(a)
puts ("#{ans}")
Skip to content
All gists
GitHub
0
0
@BraFinney BraFinney/intersection.rb Secret
Created 11 minutes ago
Code
Revisions 1
Julius and Charles
intersection.rb
arr1 = [[2,8],[5,8]]
arr2 = [[2,4],[5,4]]
arr3 = [[4,11],[8,11]]
arr4 = [[4,6],[8,6]]
ax1 = arr1[0][0]
ay1 = arr1[0][1]
ax2 = arr1[1][0]
ay2 = arr1[1][1]
ax3 = arr2[0][0]
ay3 = arr2[0][1]
ax4 = arr2[1][0]
ay4 = arr2[1][1]
bx1 = arr3[0][0]
by1 = arr3[0][1]
bx2 = arr3[1][0]
by2 = arr3[1][1]
bx3 = arr4[0][0]
by3 = arr4[0][1]
bx4 = arr4[1][0]
by4 = arr4[1][1]
#All y values in both and and in each
arr_all_y_both = [ay1,ay2,ay3,ay4,by1,by2,by3,by4]
max_all_y_both = arr_all_y_both.max
arr_all_y_a = [ay1,ay2,ay3,ay4]
max_all_y_a = arr_all_y_a.max
arr_all_y_b = [by1,by2,by3,by4]
max_all_y_b = arr_all_y_b.max
#all x values in both and in each
arr_all_x_both = [ax1,ax2,ax3,ax4,bx1,bx2,bx3,bx4]
max_all_x_both = arr_all_x_both.max
arr_all_x_a = [ax1,ax2,ax3,ax4]
max_all_x_a = arr_all_x_a.max
arr_all_x_b = [bx1,bx2,bx3,bx4]
max_all_x_b = arr_all_x_b.max
point_a_a = [ax1,ay1]
point_b_a = [ax2,ay2]
point_c_a = [ax3,ay3]
point_d_a = [ax4,ay4]
point_a_b = [bx1,by1]
point_b_b = [bx2,by2]
point_c_b = [bx3,by3]
point_d_b = [bx4,by4]
arr_all_y_a
arr_all_x_a
max_all_y_a
arr_new_a = []
#checking the x values that mat with the highest y to find the range parameters
if point_a_a[1] == max_all_y_a
arr_new_a.push(point_a_a)
end
if point_b_a[1] == max_all_y_a
arr_new_a.push(point_b_a)
end
if point_c_a[1]== max_all_y_a
arr_new_a.push(point_c_a)
end
if point_d_a[1] == max_all_y_a
arr_new_a.push(point_d_a)
end
#getting the rangr
arr_range_x = arr_new_a
arr_range_x
#range parameters
post1 = arr_range_x[0][0]
post2 = arr_range_x[1][0]
range_x = [post1,post2]
arr_all_x_b
#range parameter array
auto_gen = []
(post1..post2).each do |a|
auto_gen.push(a)
end
#x value for the intersection
#compare all the x values against
#the range parameter to see whats common
auto_gen & arr_all_x_b
#store new x as intersection for x and auto y for the highest y for rec A
inter_x = (auto_gen & arr_all_x_b).join.to_i
#joint it with highest y in rect A max_all_y_a
inter_y = max_all_y_a
#first intersection
inter_one = [inter_x,inter_y]
arr_new_b = []
if point_a_b[0] == inter_x
arr_new_b.push(point_a_b)
end
if point_b_b[0] == inter_x
arr_new_b.push(point_b_b)
end
if point_c_b[0]== inter_x
arr_new_b.push(point_c_b)
end
if point_d_b[0] == inter_x
arr_new_b.push(point_d_b)
end
#array_new_b gives us y values with the intersection 4 [4,11] [4,6]
arr_new_b
#check for y range whether it ti within the highes y point in rec A
#validate and store in new interction variable calle inter1_x,inter1_y
first_section = [arr_new_b[0][0],arr_new_b[0][1]]
second_section = [arr_new_b[1][0], arr_new_b[1][1]]
inter_two = []
if first_section[1] < max_all_y_a
inter_two.push(first_section)
elsif second_section[1] < max_all_y_a
inter_two.push(second_section)
end
#this is our new inersection after validation
inter_two
#the intersections so far
inter_one
inter_two
#compare lowest point of y
min_all_y = arr_all_y_b.min
#from out logic, min is the y value commom to both whivh we will find our range from
#find the corresnponding x cordinates of the y
range_y = []
if point_a_b[1] == min_all_y
range_y.push(point_a_b)
end
if point_b_b[1] == min_all_y
range_y.push(point_b_b)
end
if point_c_b[1] == min_all_y
range_y.push(point_c_b)
end
if point_d_b[1] == min_all_y
range_y.push(point_d_b)
end
#cordinates that have common minimu y so we can find the real range from them
#using their x cordinates.
range_y
post3 = range_y[0][0]
post4 = range_y[1][0]
#stroe the range in array
#compare to all x values in rtec A if it matches automatically it is an int
gen_range_y = []
(post3..post4).each do |a|
gen_range_y.push(a)
end
#automatic x cordinte is 5 ie the common factor in both
#automatic y value is the minimum
#store them in new array calles inter 2
new_inter_x = (gen_range_y & arr_all_x_a).join.to_i
new_inter_y = min_all_y
inter_three = [new_inter_x,new_inter_y]
inter_three
#x position of intersewction against all the x in rec A which ha s
# a y value of < the highest y in rect b > = value of the lowest y
#rec B
#position x is simple inter_three[0]
#lets compare first and store in arr
arr_common_reca = []
if point_a_a[0] == inter_three[0]
arr_common_reca.push(point_a_a)
end
if point_b_a[0] == inter_three[0]
arr_common_reca.push(point_b_a)
end
if point_c_a[0]== inter_three[0]
arr_common_reca.push(point_c_a)
end
if point_d_a[0] == inter_three[0]
arr_common_reca.push(point_d_a)
end
#returns all the values with the common inter_three x value
arr_common_reca
#lets validate whethger the y value in rec A common x val is less than the highest
#y in rec B but greater than or equal to the lowest y in rect b
#highest y max_all_y_b
#lowest y min_all_y_b
#find min_all_y_b
min_all_y_b = arr_all_y_b.min
#lets validate arr_common_reca
first_instance = [arr_common_reca[0][0], arr_common_reca[0][1]]
second_instance = [arr_common_reca[1][0], arr_common_reca[1][1]]
inter_four = []
if first_instance[1] < max_all_y_b and first_instance[1] >= min_all_y_b
inter_four.push(first_instance)
end
if second_instance[1] < max_all_y_b and second_instance[1] >= min_all_y_b
inter_four.push(second_instance)
end
inter_four
#after validation outoput shoud be inter_four
#final out put inter one, inter two, inter thjree and inter 4 in one array
print inter_real = [inter_one,inter_two,inter_three,inter_four]
@JuliusNM
Attach files by dragging & dropping or
.
Styling with Markdown is supported
Contact GitHub API Training Shop Blog About
© 2016 GitHub, Inc. Terms Privacy Security Status Help
def square_root(g,d)
lowest_point = 0
highest_point = g
middle_point = (g + 0) / 2.0
until (middle_point **2)-g <= d and (middle_point **2)-g >= 0 do
highest_point = middle_point if (middle_point **2)-g > d
lowest_point = middle_point if (middle_point **2)-g < 0
middle_point = (lowest_point + highest_point) / 2
end
middle_point
end
def prime(r,g)
value = true
number = square_root(r,g)
g = number.round
start = 2
while start <=g
if r % start == 0
value = false
end
start =start + 1
end
value
end
prime(12,0.0001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment