Defribillators 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 ?