Defibrillators puzzle discussion

[python]
I had the same issue and I was 100% that my code was good. After a while I found an error in my count_distance() formule. In my case it was the cosinus function, instead of dividing the lambda(latitude) I had cos(lambda(latitude)) / 2 :wink:
After change I have 100% score.

Please note. This also requires Degrees and not rads. It dont work with rads

Thanks, youā€™re the G, I didnā€™t think about having reversed it while filling my defib class.

I verified many times my formula and applied it in different ways but I still had this failing.

It now all passes.

Thanks bro

iā€™m stuck. What does Single possibility mean? Iā€™m stuck at 75%.

I donā€™t fully understand the problem of replace, i used the same function but i donā€™t see the issue. Can you explain it further ?
Btw,I have the same problem(only the 2 first tests work and the distance is still correct)

Still same problemā€¦
I canā€™t see the problemā€¦ here is my code if you can help :slight_smile:
https://pastebin.com/3qfGtyJa

OK, jā€™ai rĆ©ussi, cā€™Ć©tait juste ma mĆ©thode pour transformer la virgule en point qui ne marchait pas bienā€¦

Hello everyone, Im new to programming and im trying to solve the defibrillators problem.
I will share my code to you so that i will get some help.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <math.h>

typedef struct defib {
    char *name;
    double lon;
    double lat;
}def;

double distance(double lon_a, double lat_a, double lon_b, double lat_b) {
    double x, y, d;
    x = (lon_b-lon_a) * cos((lat_a+lat_b)/2);
    y = lat_b - lat_a;
    d = (sqrt(x*x+y*y)) * 6371.0;
    return d;
}

double char_to_double(char *a) {
    while(*a!='\0') {
        if(*a==',') {
            *a = '.';
        }
        a++;
    }
    return atof(a);
}

char *gotoNext(char c, char *str) {
    while(*str != c) {
        str++;
    }
    return str;
}

void getDefib(char *line, def *res) {
    line = gotoNext(';',line);
    line++;
    res->name = line;
    for(int k=0;k<3;k++) {
        line = gotoNext(';',line);        
        *line = '\0';
        line++;
    }
    char *lon = line;
    line = gotoNext(',',line);
    *line = '.';
    line = gotoNext(';',line);
    char *lat = ++line;
    line = gotoNext(',',line);
    *line = '.';
    res->lon=atof(lon);
    res->lat=atof(lat);
}   

int main(int argc, char **argv) {
    char strlon[50], strlat[50];
    int N;
    scanf("%s", strlon);
    scanf("%s", strlat);
    scanf("%d", &N);
    
    double lon = char_to_double(strlon);
    double lat = char_to_double(strlat);
    double min = DBL_MAX;
    char output[100];
    double d;
    char line[256];
    def res;
    
    for(int i=0;i<N;i++) {
        fgets(line,256,stdin);
        getDefib(line,&res);
        d = distance(lon,lat,res.lon,res.lat);
        if(d<min) {
            min=d;
            strcpy(output,res.name);
        }
    }
    printf("%s\n", output);
    return 0;
}

And this is the output :
Segmentation fault.

at Answer.c. function gotoNext (c=59 ā€˜;ā€™, str=0x7ffffffff000 <error: Cannot access memory at address 0x7ffffffff000>) on line 32

at Answer.c. function getDefib (line=0x7fffffffed13 ā€œ\033rvw\375\320x86_64ā€, res=0x7fffffffe7f0) on line 43

at Answer.c. function main (argc=1, argv=0x7fffffffeb08) on line 75

For some reason there is a segmentation fault that i cannot understand and i cannot solve for enough days.
Could somebody help me? I would higly appreciate your help.Thanks in advance.

Je vois que la plupart des personnes utilisent ā€œsqrtā€ et le nombre 6371 alors que cela ne change rien pour trouver un min. Ces opĆ©rations sont inutiles.

minBy( t => {
val x= (long - t._2) * Math.cos( (lat + t._3) / 2)
val y = (lat - t._3)
x * x + y * y
})

Yes. Pytha suffitā€¦ ;-)ā€¦

Hi,

I tried solving the puzzle with the code underneath in Python. For some reason itā€™s not working, though i think iā€™ve been precise in copying the formula. Have i been missing something? Iā€™ve changed the , in .

lb = abs(float(lon)-float(place[-2]))
la = abs(float(lat)-float(place[-1]))
x = (lb -la) * math.cos((la+lb)/2)
y = lb-la
d = ((x**2+y**2)**0.5) * 6371

Is the angle in degrees or in radians?

Thnaks for your answer.
I didnā€™t change anything. So in Python default should be radians, right?
but when i type x = (lb -la) * math.cos(math.radians(la+lb)/2)
the outcome of x is different. and when i type
x = (lb -la) * math.cos(math.degrees(la+lb)/2)
x is different as well. yet none give me a green light.
Does this have to do with the way floats are made in Python?

help radians prints:

Help on built-in function radians in module math:
radians(x, /)
    Convert angle x from degrees to radians.

Maybe your algorithm is false elsewhere too.

I donā€™t know where i made a mistake. If you want, iā€™d be gratefull if you looked. :slight_smile:

import sys
import math

defib = []

lon = input().replace(',','.') 
lat = input().replace(',','.') 
n = int(input())
for i in range(n):
    defib.append(input().split(';'))

shortest = [1000000, 'a']

for place in defib:
    place[-2] = place[-2].replace(',','.') 
    place[-1] = place[-1].replace(',','.') 
    
    lb = abs(float(lon)-float(place[-2]))
    la = abs(float(lat)-float(place[-1]))
    x = (lb-la) * (math.cos(math.radians(la+lb)/2))
    print(x, file=sys.stderr)
    y = lb-la
    d = ((x**2+y**2)**0.5) * 6371
    if d < shortest[0]:
        shortest = [d, place]
print(shortest[1][1])

Bonjour aprĆØs avoir fini le code Python, je passe 3 validateurs sur 4 et pourtant jā€™obtiens quand mĆŖme 100% lorsque je le soumets.

Lā€™erreur viens du fait que je trouve ā€œCRRā€ au troisiĆØme validateur au lieu de ā€œCaisse Primaire dā€™Assurance Maladieā€.

Quelquā€™un a une idĆ©e :)) ? (jā€™ai bel et bien ma conversion en radians)

I got all testcases right. But when i submit the code it says i failed the first testcase which is called Single possibility. But when coding the first testcase is Example. This seems to be a missing or wrong testcase.

UPPPPPP have you got the solution ?

Hello,

Like many others, I have 100% before submitting my code. Once submitted it fails on ā€œComplete file 2ā€ for a score of 75%.

Iā€™ve tried different ideas found on this forum but none of them worked for me.

I pass through every defibrilators and I removed useless parts from the formula.

I even though that because I speak french maybe my variable names were considered as harcoded stuff but itā€™s not the case.

Maybe there something else that I forgot, Iā€™m using C#.

Thanks in advance for your help!

Iā€™m a bit lost Iā€™ve saw post about radians and degrees but I donā€™t know if I should use it because my results are worst. My original code gives me those results for the top 10 nearest defibrilators (name and distance) for test case ā€œComplete file 2ā€. (Code is in C#) I guess distance results should give an hint on my error, the results are without * 6371.

Amphitheatre dā€™O 0.000757519420174854
Montpellier Ecole National Superieure de Chimie (Laboratoire) 0.00293015136295231
Les Jardins de Grasse 0.00297627200519735
Montpellier Ecole National Superieure de Chimie 0.00344800373048659
France Telecom 0.00400535240319536
Universite Montpellier 1 UFR Odontologie (Hall Premier Etage Bat A) 0.00416798383896154
Caisse Primaire dā€™Assurance Maladie 0.00529942436305899
Hotel des impots 0.00578389486554183
Batiment k Iut montpellier 2 0.00690624732932209
Centre de formation professionnel croix rouge 0.007014880161973