Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,518 members, 7,999,313 topics. Date: Monday, 11 November 2024 at 12:55 AM

Regular Programming Challenges With The Odd Money To Win - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Regular Programming Challenges With The Odd Money To Win (2481 Views)

Codewars.com Coding Challenges / Programming Challenges / New To Python, Gurus Please Help Solve These Challenges. Thanks. (2) (3) (4)

(1) (2) (Reply) (Go Down)

Regular Programming Challenges With The Odd Money To Win by faucon(m): 3:32pm On Nov 12, 2019
I want to use this as a medium to be posting regular programming challenges to stimulate our brains and just for fun.

Perhaps you can use this as an opportunity to improve your problem-solving skills and to improve your skills in an existing or a new programming language.

To spice things up, occasionally there will be challenges with deadlines in which money can be won. There can only be 1 winner and it will be the first person to answer all the problems in the challenge correctly. You’ll receive the money through PayPal or bank transfer

You can submit your answers in any programming language but obviously I’d prefer C, Java or Python only because it’s easier for me.

I will be publishing the first set of challenges once I’m done finalising the questions. This one will have somebody winning £50 if the correct answers are put forward within 5 days.

Before I send out the first challenge, I’d give some time to answer any questions or provide more clarifications if required.
Re: Regular Programming Challenges With The Odd Money To Win by General0847: 3:51pm On Nov 12, 2019
Interesting.
Re: Regular Programming Challenges With The Odd Money To Win by shotin(m): 3:57pm On Nov 12, 2019
hmm.... That's good, waiting for more update...
Re: Regular Programming Challenges With The Odd Money To Win by stanliwise(m): 5:09pm On Nov 12, 2019
let me nor say anything.
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 6:29pm On Nov 12, 2019
stanliwise:
let me nor say anything.
no you have to say something now tongue
Re: Regular Programming Challenges With The Odd Money To Win by stanliwise(m): 9:58pm On Nov 12, 2019
faucon:

no you have to say something now tongue
What's your aim?, why are we submitting codes to you? Why are you paying us?
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 11:45pm On Nov 12, 2019
stanliwise:
What's your aim?, why are we submitting codes to you? Why are you paying us?
To stimulate things here and ascertain people's approach to solving problems and reward occasionally. Perhaps scout for future projects I may have.
Re: Regular Programming Challenges With The Odd Money To Win by ANTONINEUTRON(m): 6:59am On Nov 13, 2019
wen are u posting the first challenge
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 11:34am On Nov 13, 2019
ANTONINEUTRON:
wen are u posting the first challenge
first challenge coming in less than an hour
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 12:37pm On Nov 13, 2019
First Challenge
Please post all the answers to the questions in one go, it will be easier for me and everyone else to go through and will avoid having to collate pieces of your answers in the thread.

Only the second question under Quiz doesn’t require you to code your answer.

As for which programming language to use, preferably C, Java or Python but you’re free to use whichever you’re comfortable with.

There is only 1 winner. First to get all answers correctly before November 20 by 23:59 PM wins £50. I’ll send it to the winner’s PayPal account if they have one or the equivalent in Naira to their Nigerian account.

Quiz

1. We denote "A in base b": Ab. What is the result, in base 10, of 102 + 108 + 1016?
Note only the value in base 10, without specifying the base.

2. Which of these dates cannot be represented using a timestamp on a UNIX system?
a. 2 March 1969
b. 2 December 1970
c. 13 January 1997
d. 25 August 1991
e. I don't know

3. If you write all the numbers from 1 to 999 in a row by separating the numbers by points (1.2.3.4.5.6.7.8.9.1.0.1.1.1.2 ...), how many points will be immediately surrounded by odd numbers?

4. What is the 1000th integer that has the number of 1's as a prime number in its binary representation?
Reminder: 1 is not first.

5. What is the biggest whole number strictly lower than 421337 that generates the longest Collatz conjecture?

Problem

Subject

Dayo's car has a technical problem: his right turn signal is constantly working, impossible to stop. Wanting to respect the rules of the road, he decides to always take the first right.

Being equipped with a state-of-the-art car model with an integrated navigation assistant, Dayo asks you to write an application that tells him which direction to take when he arrives at a roundabout.

You are given the coordinates of the roundabout, and a list of coordinates representing the different destinations of the roundabout exits. The car arrives at the roundabout from the first of these destinations in the list. Write a function that returns the coordinates of the destination of the exit that corresponds, from Dayo's point of view, to the first exit of the roundabout.

Input
The Input will include:

• on the first line, the X, Y coordinates of the roundabout, separated by a space;
• on the second line, the number N of exits on the roundabout;
• on the third line, the coordinates X1, Y1 of the part of the road in which the car arrives, separated by a space;
• on the following N - 1 lines, the coordinates Xi, Yi of the destinations of each of the exits, separated by a space.

Output
You will display in your output:

• the coordinates of the destination of the exit corresponding to the first exit of the roundabout.

Constraints
• 2 ≤ N ≤ 10,001;
• each coordinate is a whole number and between -10,000 and 10,000;
• the coordinates all lead to different directions: the roads (the right segments from the roundabout to their destinations) do not overlap.

Runtime constraints
Maximum memory usage: 5000 kilobytes
Maximum execution time: 500 milliseconds

Input/output samples
Sample input
1 1
4
1 0
1 2
0 1
2 1

Sample output
2 1

Note
Refer to image.

Re: Regular Programming Challenges With The Odd Money To Win by OutOfTheAshes(m): 7:40pm On Nov 13, 2019
Where are the developers... food don done o grin
Re: Regular Programming Challenges With The Odd Money To Win by silento(m): 8:57pm On Nov 13, 2019
Waiting for people
Re: Regular Programming Challenges With The Odd Money To Win by ANTONINEUTRON(m): 10:25pm On Nov 13, 2019
That last problem though....
Re: Regular Programming Challenges With The Odd Money To Win by ANTONINEUTRON(m): 3:05pm On Nov 14, 2019
Advance Data structure is one thing local man will be learning next semester, and the way he'll focus on it.....

At this point. I throw in the towel. But I achieved something at least.
That last problem is a no go area for me. As for question.4, It's not clear to me.
1. Using java.

public class binaryProblem {
public static int getBase10Value(String num){
int len = num.length()-1;
String b = Character.toString(num.charAt(len));
Integer base = Integer.parseInt(b);
String newNum = num.replace(b,"" ) ;
Integer numberInItsBase = Integer.parseInt(newNum,base);
String numberInBase10 = Integer.toString(numberInItsBase,10);
return Integer.parseInt(numberInBase10);
}


public static void main(String[] args) {
String a = "102";
String b = "108";
String c = "1016";
int x = getBase10Value(a);
int y = getBase10Value(b);
int z = getBase10Value(c);
System.out.print(x+y+z);
}
}

Output: 47
Note: The program takes the last number 1016 as base 6.

2. option a is the answer. Because Unix timestamp starts counting at 1st, January, 1979

3.Using java

import java.util.ArrayList;
public class pointBetweenOddNum{
public static int getNextNumber(int currentIndex, ArrayList<Integer> arr){
int nextNum = 0;
if((currentIndex+1) != arr.size()){
nextNum = arr.get(currentIndex+1);
}
return nextNum;
}

public static ArrayList<Integer> getList(){
ArrayList<Integer> arr = new ArrayList<Integer>();
int num;
for(int i=1; i<1000; i++){
if(i<100 && i>9){
arr.add(i/10);
arr.add(i%10);
}else if(i>99 && i<1000){
arr.add(i/100);
int j = i%100;
arr.add(j/10);
arr.add(j%10);
}else{
arr.add(i);
}
}
return arr;
}

public static void main(String[] args) {
int count=0;
ArrayList<Integer> arr = getList();
for(int i=0; i<arr.size(); i++){
int next = getNextNumber(i,arr);
int current = arr.get(i);
int remainder = (current+next)%2;
if(remainder==0){
count++;
}
}
System.out.println(count);
}
}

Output: 1440

4. Not clear. Because all 1000th numbers have 1 in their binary representation.

5.Using java

public class test4 {
public static int getLength(int num){
int len = 0;
for(int i=1; num>2; i++){
int remainder = num%2;
if(remainder==0){
num /= 2;
}else{
num=(num*3)+1;
}
len=i;
}
return len;
}

public static void main(String[] args) {
int maxNum,maxNumLength;
int limit = 421337;
maxNum = 0;
maxNumLength = 0;
for(int i=10; i<limit; i++){
int len = getLength(i);
if(len>maxNumLength){
maxNum = i;
}
}
System.out.println(maxNum);
}
}

Output: 421336
6. There's time for everything in this life. Please after this competition ends explain how this problem can be solve.

Thank you.
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 4:57pm On Nov 14, 2019
@ANTONINEUTRON
Good going man on being the first to show some result. Don't throw in the towel yet.

1. It's not correct. Please look at the bases for each number. Sorry I didn't format it correctly at first.
Also pay attention to the curly brackets, I had to restructure them before running your code.

2. You're right but care to tell us why?

3. It's not correct

4. I'm asking for the 1000th integer that has the number of 1's as a prime number in its binary representation.

5. Your approach is not entirely wrong but you're incorrect. You're close though.

6. Yeah sure. I'm also quite interested to see how people will solve it. I used a naive approach and probably someone may come up with something better.
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 10:07pm On Nov 16, 2019
I was expecting more answers to be coming in right about this time, what's happening?
Re: Regular Programming Challenges With The Odd Money To Win by gbolly1151(m): 8:04am On Nov 17, 2019
Hope am not too late to the party?
Re: Regular Programming Challenges With The Odd Money To Win by OutOfTheAshes(m): 9:36am On Nov 17, 2019
faucon:
I was expecting more answers to be coming in right about this time, what's happening?
Because we have more developers than programmers.
Those questions require a high level of problem solving skill. I am trying to wrap my head around them. Don't expect much from Nairalander developers.
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 12:48pm On Nov 17, 2019
No one is late to the party still. Also with the new info I have got, I have decided to extend the deadline to November 20. I'll update the main post.

1 Like

Re: Regular Programming Challenges With The Odd Money To Win by gbolly1151(m): 2:12pm On Nov 17, 2019
Here is my answers,i will be updating them as i will be solving them

I am coding in python

1.

def A_base_B(num):
num[0]=str(num[0])
AbaseB=0
for i in range(len(num[0])):
value=int(num[0][len(num[0])-(i+1)])*(num[1]**i)
AbaseB += value
return AbaseB

if __name__== '__main__':
a=[ '10' , 2 ] # 10 base 2
b=[ '10' , 8 ] # 10 base 8
c=[ '10' , 16 ] # 10 base 16

#collect the base numbers into a list

base=[ a,b,c ]

#result of addition of a,b,c
result=0

for i in base:
r=A_base_B(i)
result += r
print(result)


Output : 26

2. The answer is (a) UNIX timestamp system start with 1,January 1970 , all dates before that will result in negative value


3.

numbers = ' '
for num in range(1,1000):
numbers += str(num)

numbers=list(numbers)

point_count = 0
odd_num = 0

for odd in range(1,len(numbers)):
numbers[odd] = int(numbers[odd])
if numbers [odd] == 1 or numbers[odd]%2 == 1:
if numbers [odd - 1] == odd_num:
point_count += 1
odd_num = numbers [odd]

print(point_count)


Output: 775

4.
I am trying to analyze is this way, the 1st integer number with 1's as prime number in its binary is 3, the 2nd integer is 7

The this code will work for the 1000th integer



def prime(num):
if num > 1:
for i in range(2,num//2):
if (num % i) == 0:
return False
break
else: return True
else: return False

#target is the nth term of prime with 1's in its binary
def n_term(target=1):
binary = '0b'
count = 0
while count < target:
binary += '1'
deci=int(binary,2)
if prime(deci):
count += 1
return deci

print(n_term())



Got into an inifinite loop for valves greater than 8 on my PC

For output of 1st,2nd and 3rd integer The output are 3,7,31 respectively
Re: Regular Programming Challenges With The Odd Money To Win by Kaylhey20: 11:04pm On Nov 17, 2019
Hi everyone, I tried solving the questions and I arrived at answers for all of them.

Although my algorithm may not be the best, I'm just in my first semester of studying Comp. Science in the US. So, the answers are as follows:

1) Output is 26
2) The epoch for time reference on a UNIX system is 1st January, 1970. So option a) will result in negative time . ;( ,
Answer is (a).
3) Output is 775
4)Output is 1990
5)Output is 410011
6) I solved it using the concept of gradients, and my output is 2 , 1 as stated.

I tried posting my code, but the formating is bad. OP if you can check through my answers, I can share you the file and you can go through my code.

Thanks.

#QUESTION 1
def toBaseTen(number, base):
number = str(number)
i = len(number) - 1
result = 0
for n in number:
result += int(n) * (base ** i)
i = i - 1
return result

first = toBaseTen(10, 2)
second = toBaseTen(10, cool
third = toBaseTen(10, 16)
print(first + second + third) #Outputs 26

#QUESTION 2
'''
The answer is (a) because the epoch (time reference) for a UNIX system is 1st January, 1970.
Therefore, all time that can represented must be after this date.
So, all options except (a) doesn't follow the above rule
'''

#QUESTION 3
numbers_in_str = ''
for i in range(1, 1000):
numbers_in_str+=str(i)

numbers_with_dot = ''
for num in numbers_in_str:
numbers_with_dot +=num + '.'

last_index = len(numbers_with_dot) - 1
numbers_with_dot = numbers_with_dot[:last_index]

def isOdd(n):
return n % 2 != 0
count = 0
for j in range(len(numbers_with_dot)):
if numbers_with_dot[j] == '.':
num_before = int(numbers_with_dot[j - 1])
num_after = int(numbers_with_dot[j + 1])
if isOdd(num_before) and isOdd(num_after):
count+=1
print(count)#Outputs 775


#QUESTION 4
def primeNumber(num):
if num > 1:
for i in range(2, num):
if num % i == 0:
return False
return True
else:
return False
def toBinary(num):
result = ''
while num != 0:
rem = num % 2
num = num // 2
result = str(rem) + result
return result
#program execution starts.
count = 0
i = 1
while count < 1000:
result = toBinary(i)
no_of_ones = result.count('1')
if primeNumber(no_of_ones):
count+=1
i +=1
print(i-1)#Outputs 1990

#QUESTION 5
def generateCollatz_length(num):
if num > 1:
num_list = [num]
while num != 1:
if isOdd(num):
num = (num * 3) + 1
else:
num = num // 2
num_list.append(num)
return len(num_list)
else:
return None
#program execution starts
max_length = 0
whole_int = 0
for i in range(2, 421337):
length = generateCollatz_length(i)
if length > max_length:
max_length = length
whole_int = i
print(whole_int)#Output 410011

#QUESTION 5
'''
Pardon me on this one, I don't really know how to work with these kind of inputs, although I've done some
challenges on hackerrank, I can't implement them on my computer.
'''
'''
Basically, I will be converting the inputs to a list of tuples, with.
1) First line will be saved as a tuple variable.
2) N as the length of the list.
3) the next N lines as tuples (coordinates) in the list ... with the first element the (x1, y1) of the car's location.
'''
#The sample input can be represented by:
sample_input = [(1, 0), (1, 2), (0, 1), (2, 1)]
round_about = (1,1)

#I'm using the concept of gradients.
#Since he is always turning to the right, the gradient should be positive relaive to the car's position.
#Then I'll find the positive gradient with the lowest value to ascertain the exit closest to the car.

car_location = sample_input[0]
car_x = car_location[0]
car_y = car_location[1]
value = 100000
for i in range(1, len(sample_input)):
x = sample_input[i][0]
y = sample_input[i][1]
change_in_y = y - car_y
change_in_x = x - car_x
try:
gradient = change_in_y / change_in_x
except ZeroDivisionError:
gradient = 0
if gradient > 0 and gradient < value: #check if gradient is positive
result = sample_input[i]
value = gradient

print(result)

2 Likes

Re: Regular Programming Challenges With The Odd Money To Win by gbolly1151(m): 9:06am On Nov 18, 2019
Hmm....loving those questions
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 11:26am On Nov 18, 2019
gbolly1151 and Kaylhey20 both of you are getting very close. But which of you would arrive first ? wink

Kaylhey20 use the hashtag sign to format your code properly or write your code inside "["code"][\code]" (remove quotes)

It would come out like this


#include <iostream>

// Define a generic STUB template
#define STUB(function_name) int function_name() { \
std::cerr << "FIXME: Finish " #function_name << std::endl; \
return -1; \
}

// Use our STUB template to define stubs for two functions
STUB(GetUserNum)
STUB(ComputeAvg)

int main()
{
// Calling one of the stubs
std::cout << "There are " << GetUserNum() << " users.";
return 0;
}
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 5:25pm On Nov 18, 2019
gbolly1151:
Here is my answers,i will be updating them as i will be solving them

I am coding in python

1.

def A_base_B(num):
num[0]=str(num[0])
AbaseB=0
for i in range(len(num[0])):
value=int(num[0][len(num[0])-(i+1)])*(num[1]**i)
AbaseB += value
return AbaseB

if __name__== '__main__':
a=[ '10' , 2 ] # 10 base 2
b=[ '10' , 8 ] # 10 base 8
c=[ '10' , 16 ] # 10 base 16

#collect the base numbers into a list

base=[ a,b,c ]

#result of addition of a,b,c
result=0

for i in base:
r=A_base_B(i)
result += r
print(result)


Output : 26

2. The answer is (a) UNIX timestamp system start with 1,January 1970 , all dates before that will result in negative value


3.

numbers = ' '
for num in range(1,1000):
numbers += str(num)

numbers=list(numbers)

point_count = 0
odd_num = 0

for odd in range(1,len(numbers)):
numbers[odd] = int(numbers[odd])
if numbers [odd] == 1 or numbers[odd]%2 == 1:
if numbers [odd - 1] == odd_num:
point_count += 1
odd_num = numbers [odd]

print(point_count)


Output: 775

4.
I am trying to analyze is this way, the 1st integer number with 1's as prime number in its binary is 3, the 2nd integer is 7

The this code will work for the 1000th integer



def prime(num):
if num > 1:
for i in range(2,num//2):
if (num % i) == 0:
return False
break
else: return True
else: return False

#target is the nth term of prime with 1's in its binary
def n_term(target=1):
binary = '0b'
count = 0
while count < target:
binary += '1'
deci=int(binary,2)
if prime(deci):
count += 1
return deci

print(n_term())



Got into an inifinite loop for valves greater than 8 on my PC

For output of 1st,2nd and 3rd integer The output are 3,7,31 respectively
Your approach to the 4th question is not correct. To help you out a bit the sequence is 3,5,6. Refactor your code a bit and use print statements to see where you're going wrong. That's all I can say for now.
Re: Regular Programming Challenges With The Odd Money To Win by Kaylhey20: 5:45pm On Nov 18, 2019
Thanks, @faucon.

I have updated it, but are the answers correct.

1 Like

Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 7:29pm On Nov 18, 2019
Kaylhey20:
Thanks, @faucon.

I have updated it, but are the answers correct.
Congratulations, you're the first to answer all the questions correctly.

How long did it take you to answer all the questions?

I will PM you for your £50 prize.

1 Like

Re: Regular Programming Challenges With The Odd Money To Win by 4kings: 7:51pm On Nov 18, 2019
Awesome job guys!
Re: Regular Programming Challenges With The Odd Money To Win by Nobody: 8:00pm On Nov 18, 2019
Thief! Only out to steal source codes.
Proof of prices won?
Re: Regular Programming Challenges With The Odd Money To Win by Kaylhey20: 8:41pm On Nov 18, 2019
faucon:

Congratulations, you're the first to answer all the questions correctly.

How long did it take you to answer all the questions?

I will PM you for your £50 prize.
About an hour and 30 minutes, but the last one took the longest time.
Re: Regular Programming Challenges With The Odd Money To Win by gbolly1151(m): 8:42pm On Nov 18, 2019
Congrat @kaylhey20
Re: Regular Programming Challenges With The Odd Money To Win by gbolly1151(m): 8:47pm On Nov 18, 2019
I really learn alot from this challenge let it keep coming @faucon
Re: Regular Programming Challenges With The Odd Money To Win by faucon(m): 10:27pm On Nov 18, 2019
Kaylhey20:

About an hour and 30 minutes, but the last one took the longest time.
I have replied your PM please check.

(1) (2) (Reply)

Please Help An Upcoming Programmer / Contribute What You Know About Artificial Intelligence / How To Get A User IP Address With Javascript

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 62
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.