Task: Determine if a string has all unique characters







Python: 

from itertools import groupby



def duplicatedCharIndices(s):
    
    def go(xs):
        if 1 < len(xs):
            duplicates = list(filter(lambda kv: 1 < len(kv[1]), [
                (k, list(v)) for k, v in groupby(
                    sorted(xs, key=swap),
                    key=snd
                )
            ]))
            return Just(second(fmap(fst))(
                sorted(
                    duplicates,
                    key=lambda kv: kv[1][0]
                )[0]
            )) if duplicates else Nothing()
        else:
            return Nothing()
    return go(list(enumerate(s)))




def main():
    

    def showSample(s):
        return repr(s) + ' (' + str(len(s)) + ')'

    def showDuplicate(cix):
        c, ix = cix
        return repr(c) + (
            ' (' + hex(ord(c)) + ') at ' + repr(ix)
        )

    print(
        fTable('First duplicated character, if any:')(
            showSample
        )(maybe('None')(showDuplicate))(duplicatedCharIndices)([
            '', '.', 'abcABC', 'XYZ ZYX',
            '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ'
        ])
    )






def fTable(s):
    
    def go(xShow, fxShow, f, xs):
        ys = [xShow(x) for x in xs]
        w = max(map(len, ys))
        return s + '\n' + '\n'.join(map(
            lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
            xs, ys
        ))
    return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
        xShow, fxShow, f, xs
    )





def Just(x):
    
    return {'type': 'Maybe', 'Nothing': False, 'Just': x}



def Nothing():
    
    return {'type': 'Maybe', 'Nothing': True}



def fmap(f):
    
    return lambda xs: [f(x) for x in xs]



def fst(tpl):
    
    return tpl[0]



def head(xs):
    
    return xs[0] if isinstance(xs, list) else next(xs)



def maybe(v):
    
    return lambda f: lambda m: v if (
        None is m or m.get('Nothing')
    ) else f(m.get('Just'))



def second(f):
    
    return lambda xy: (xy[0], f(xy[1]))



def snd(tpl):
    
    return tpl[1]



def swap(tpl):
    
    return (tpl[1], tpl[0])



if __name__ == '__main__':
    main()




Fortran: program demo_verify
implicit none
    call nodup('')
    call nodup('.')
    call nodup('abcABC')
    call nodup('XYZ ZYX')
    call nodup('1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ')
contains

subroutine nodup(str)
character(len=*),intent(in)  :: str
character(len=*),parameter   :: g='(*(g0))'
character(len=:),allocatable :: ch
integer                      :: where
integer                      :: i
   where=0
   ch=''

   do i=1,len(str)-1
      ch=str(i:i)
      where=index(str(i+1:),ch)
      if(where.ne.0)then
         where=where+i
         exit
      endif
   enddo

   if(where.eq.0)then
     write(*,g)'STR: "',str,'"',new_line('a'),'LEN: ',len(str),'. No duplicate characters found'
   else
     write(*,g)'STR: "',str,'"'
     write(*,'(a,a,t1,a,a)')repeat(' ',where+5),'^',repeat(' ',i+5),'^'
     write(*,g)'LEN: ',len(str), &
     & '. Duplicate chars. First duplicate at positions ',i,' and ',where, &
     & ' where a ','"'//str(where:where)//'"(hex:',hex(str(where:where)),') was found.'
   endif
   write(*,*)

end subroutine nodup

function hex(ch) result(hexstr)
character(len=1),intent(in) :: ch
character(len=:),allocatable :: hexstr
   hexstr=repeat(' ',100)
   write(hexstr,'(Z0)')ch
   hexstr=trim(hexstr)
end function hex

end program demo_verify