Dyskusja:REGON

message: O ile wiadmo mi występuje też regon rozszerzony 9-cyrf podstawowy. a rozszerzony ma 13lub14cyfr. szukam właśnie takiej informacji.
Pozdrawiam
Grzegorz Duda
* Rozszerzenia regonu są 5-cyfrowe (w sumie 14 cyfr). Nadaje się je w celu zidentyfikowania tzw. jednostek lokalnych - np. poszczególnych oddziałów banków. Sosna
Kod w Pythonie nie uwzględnia sytuacji w której wynik modulo będzie równy 10. Poprawiam... ale to jestbezpośrednio nie możliwe do zrealizowania
No dobra, ale do czego służy ten REGON?
thread_title: Untitled

message: === Implementacja algorytmu w języku Python ===
def check_REGON(regon): 
	weights = {
		7: (2, 3, 4, 5, 6, 7),
		9: (8, 9, 2, 3, 4, 5, 6, 7),
		14: (2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8),
	}
	try:
		regon     = map(int, regon)
		weights   = weights[len(regon)]
	except:
		return False     
	checksum = sum(n * w for n, w in zip(regon, weights))
	  
	return checksum % 11 % 10 == regon[-1]
=== Implementacja algorytmu w języku Ruby ===
  def valid_regon?(regon_string)
    case regon_string.size
    when 7
      weights = [2, 3, 4, 5, 6, 7]
    when 9
      weights = [8, 9, 2, 3, 4, 5, 6, 7]
    when 14
      weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
    else
      return false
    end
    regon_string = regon_string.split(//).map {|d| d.to_i }
    checksum = 0
    weights.each_with_index { |w, i| checksum += w * regon_string[i] }
    return checksum % 11 % 10 == regon_string.last
  end
====alternatywna implementacja algorytmu w języku Ruby====
class String
    def valid_regon?
        if length === 7
            return ('00' + self).valid_regon?
        elsif length === 9
            weights = [8, 9, 2, 3, 4, 5, 6, 7]
            regon = (split //).collect &:to_i
            checksum = weights.inject (0) { |sum, weight| sum + weight * regon.shift }
            return checksum % 11 % 10 === regon.shift
        elsif length === 14
            weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
            regon = (split //).collect &:to_i
            checksum = weights.inject { |sum, weight| sum + weight * regon.shift }
            return self[0,9].valid_regon? && checksum % 11 % 10 === regon.shift
        else
            return false
        end
    end
end
=== Implementacja algorytmu w języku PHP ===
function CheckRegon( $regon ) 
{
	switch( strlen( $regon ) )
	{
		case 7:
			$weights = array(2, 3, 4, 5, 6, 7);
			break;
		case 9:
			$weights = array(8, 9, 2, 3, 4, 5, 6, 7);
			break;
		case 14:
			$weights = array(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8);
			break;
		default:
			return false;
	}
	
	$checksum = 0;
	foreach( $weights as $i => $w )
	{
		$checksum += $w * $regon{$i};
	}
	 
	return $checksum % 11 % 10 == substr( $regon, -1, 1 );
}
=== Implementacja algorytmu w języku PL/SQL ===
CREATE OR REPLACE FUNCTION PKCORE."WALIDUJ_REGON" (p_regon varchar) return varchar2 as
crc number;
v_last number;
begin
if length(p_regon) = 9 then
crc:= 8 * to_number(substr(p_regon,1,1))
    + 9 * to_number(substr(p_regon,2,1))
    + 2 * to_number(substr(p_regon,3,1))
    + 3 * to_number(substr(p_regon,4,1))
    + 4 * to_number(substr(p_regon,5,1))
    + 5 * to_number(substr(p_regon,6,1))
    + 6 * to_number(substr(p_regon,7,1))
    + 7 * to_number(substr(p_regon,8,1));
    v_last:=to_number(substr(p_regon,9,1));
elsif length(p_regon) = 14 then
crc:=2 * to_number(substr(p_regon,1,1))
    +4 * to_number(substr(p_regon,2,1))
    +8 * to_number(substr(p_regon,3,1))
    +5 * to_number(substr(p_regon,4,1))
    +0 * to_number(substr(p_regon,5,1))
    +9 * to_number(substr(p_regon,6,1))
    +7 * to_number(substr(p_regon,7,1))
    +3 * to_number(substr(p_regon,8,1))
    +6 * to_number(substr(p_regon,9,1))
    +1 * to_number(substr(p_regon,10,1))
    +2 * to_number(substr(p_regon,11,1))
    +4 * to_number(substr(p_regon,12,1))
    +8 * to_number(substr(p_regon,13,1));
    v_last:=to_number(substr(p_regon,14,1));
else
    return('ERR');
end if;
    crc:=mod(crc,11);
    if crc=10 then
        crc:=0;
    end if;
    if v_last = crc then
        return 'OK';
    else
        return 'ERR';
    end if;
    exception when others then return 'ERR';
end;
thread_title:  Do przeniesienia na wikizrodla 

message: def check_REGON(regon): 
	weights = {
		7: (2, 3, 4, 5, 6, 7),
		9: (8, 9, 2, 3, 4, 5, 6, 7),
		14: (2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8),
	}
	try:
		regon     = map(int, regon)
		weights   = weights[len(regon)]
	except:
		return False     
	checksum = sum(n * w for n, w in zip(regon, weights))
	  
	return checksum % 11 % 10 == regon[-1]
thread_title:  Implementacja algorytmu w języku [[Python]] 

message: def valid_regon?(regon_string)
    case regon_string.size
    when 7
      weights = [2, 3, 4, 5, 6, 7]
    when 9
      weights = [8, 9, 2, 3, 4, 5, 6, 7]
    when 14
      weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
    else
      return false
    end
    regon_string = regon_string.split(//).map {|d| d.to_i }
    checksum = 0
    weights.each_with_index { |w, i| checksum += w * regon_string[i] }
    return checksum % 11 % 10 == regon_string.last
  end
====alternatywna implementacja algorytmu w języku Ruby====
class String
    def valid_regon?
        if length === 7
            return ('00' + self).valid_regon?
        elsif length === 9
            weights = [8, 9, 2, 3, 4, 5, 6, 7]
            regon = (split //).collect &:to_i
            checksum = weights.inject (0) { |sum, weight| sum + weight * regon.shift }
            return checksum % 11 % 10 === regon.shift
        elsif length === 14
            weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
            regon = (split //).collect &:to_i
            checksum = weights.inject { |sum, weight| sum + weight * regon.shift }
            return self[0,9].valid_regon? && checksum % 11 % 10 === regon.shift
        else
            return false
        end
    end
end
thread_title:  Implementacja algorytmu w języku [[Ruby]] 

message: class String
    def valid_regon?
        if length === 7
            return ('00' + self).valid_regon?
        elsif length === 9
            weights = [8, 9, 2, 3, 4, 5, 6, 7]
            regon = (split //).collect &:to_i
            checksum = weights.inject (0) { |sum, weight| sum + weight * regon.shift }
            return checksum % 11 % 10 === regon.shift
        elsif length === 14
            weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]
            regon = (split //).collect &:to_i
            checksum = weights.inject { |sum, weight| sum + weight * regon.shift }
            return self[0,9].valid_regon? && checksum % 11 % 10 === regon.shift
        else
            return false
        end
    end
end
thread_title: alternatywna implementacja algorytmu w języku [[Ruby]]

message: function CheckRegon( $regon ) 
{
	switch( strlen( $regon ) )
	{
		case 7:
			$weights = array(2, 3, 4, 5, 6, 7);
			break;
		case 9:
			$weights = array(8, 9, 2, 3, 4, 5, 6, 7);
			break;
		case 14:
			$weights = array(2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8);
			break;
		default:
			return false;
	}
	
	$checksum = 0;
	foreach( $weights as $i => $w )
	{
		$checksum += $w * $regon{$i};
	}
	 
	return $checksum % 11 % 10 == substr( $regon, -1, 1 );
}
thread_title:  Implementacja algorytmu w języku [[PHP]] 

message: CREATE OR REPLACE FUNCTION PKCORE."WALIDUJ_REGON" (p_regon varchar) return varchar2 as
crc number;
v_last number;
begin
if length(p_regon) = 9 then
crc:= 8 * to_number(substr(p_regon,1,1))
    + 9 * to_number(substr(p_regon,2,1))
    + 2 * to_number(substr(p_regon,3,1))
    + 3 * to_number(substr(p_regon,4,1))
    + 4 * to_number(substr(p_regon,5,1))
    + 5 * to_number(substr(p_regon,6,1))
    + 6 * to_number(substr(p_regon,7,1))
    + 7 * to_number(substr(p_regon,8,1));
    v_last:=to_number(substr(p_regon,9,1));
elsif length(p_regon) = 14 then
crc:=2 * to_number(substr(p_regon,1,1))
    +4 * to_number(substr(p_regon,2,1))
    +8 * to_number(substr(p_regon,3,1))
    +5 * to_number(substr(p_regon,4,1))
    +0 * to_number(substr(p_regon,5,1))
    +9 * to_number(substr(p_regon,6,1))
    +7 * to_number(substr(p_regon,7,1))
    +3 * to_number(substr(p_regon,8,1))
    +6 * to_number(substr(p_regon,9,1))
    +1 * to_number(substr(p_regon,10,1))
    +2 * to_number(substr(p_regon,11,1))
    +4 * to_number(substr(p_regon,12,1))
    +8 * to_number(substr(p_regon,13,1));
    v_last:=to_number(substr(p_regon,14,1));
else
    return('ERR');
end if;
    crc:=mod(crc,11);
    if crc=10 then
        crc:=0;
    end if;
    if v_last = crc then
        return 'OK';
    else
        return 'ERR';
    end if;
    exception when others then return 'ERR';
end;
thread_title:  Implementacja algorytmu w języku [[PL/SQL]]