RobinHankin / permutations

https://robinhankin.github.io/permutations/
5 stars 3 forks source link

as.character() not very good #45

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

Look:

 (x <- rperm())
 [1] (1743652)     (153)(24)(67) (1642)(57)    (34)(576)     (1765234)    
 [6] (12)(576)     (26)(45)      (147356)      (14276)(35)   (124)(37)(56)
[coerced from word form]
 as.character(x)
 [1] "7" "5" "6" "1" "7" "2" "1" "4" "4" "2" "1" "4" "1" "2" "3" "1" "6" "2" "7"
[20] "4" "6" "1" "3" "4" "4" "3" "3" "5" "5" "7" "3" "2" "2" "3" "1" "4" "5" "7"
[39] "2" "1" "2" "3" "7" "7" "2" "7" "4" "6" "3" "6" "5" "7" "4" "5" "5" "5" "2"
[58] "1" "1" "5" "4" "6" "5" "6" "6" "6" "7" "3" "6" "3"

I would expect as.character() to return something like

c("(1743652)", "(153)(24)(67)", "(1642)(57)", "(34)(576)", "(1765234)", 
"(12)(576)", "(26)(45)", "(147356)", "(14276)(35)", "(124)(37)(56)")

We should use the return value of print.cycle().

RobinHankin commented 1 year ago

Issue #45 was somewhat misleading: the problem above was caused by the fact that rperm() returns a word, and as.character() was doing its best to coerce an integer matrix to character. In any event, this needs to be documented. Observe:

> a <- rperm()
> a
 [1] (16475)(23)   (12374)(56)   (12563)(47)   (134)(27)     (157)(34)    
 [6] (1352467)     (1732456)     (156)(23)(47) (256734)      (16723)      
[coerced from word form]
> as.character(a)
 [1] "6" "2" "2" "3" "5" "3" "7" "5" "1" "6" "3" "3" "5" "7" "2" "4" "4" "3" "5"
[20] "3" "2" "7" "1" "4" "4" "5" "2" "2" "4" "1" "7" "1" "7" "1" "3" "6" "5" "7"
[39] "2" "4" "1" "6" "6" "5" "7" "2" "6" "6" "6" "5" "4" "5" "3" "6" "6" "7" "1"
[58] "1" "7" "7" "5" "4" "4" "2" "1" "1" "3" "4" "3" "2"
> as.character(as.cycle(a))
 [1] "(1,6,4,7,5)(2,3)"  "(1,2,3,7,4)(5,6)"  "(1,2,5,6,3)(4,7)" 
 [4] "(1,3,4)(2,7)"      "(1,5,7)(3,4)"      "(1,3,5,2,4,6,7)"  
 [7] "(1,7,3,2,4,5,6)"   "(1,5,6)(2,3)(4,7)" "(2,5,6,7,3,4)"    
[10] "(1,6,7,2,3)"      
> 
RobinHankin commented 1 year ago

Fixed in 5e55399