douxiji8707 2012-04-10 11:36
浏览 42

PHP使用userid创建1到9的唯一编号

In order to color code each row of a calendar I'd like to be able to create a unique integer from 1 to 9 using the field userid. I need to use the color in another part of the calendar to display the student assigned to the instructor.

Bear in mind that userID can be any length.

At first I thought of using the least significant digit, but that won't work for user IDs 01, 21, 22 etc.

Then I thought of adding the left to the right, eg userID 22 = 2+2=4, but that won't work either.

Any ideas how I might create a unique 0-9 integer based on the users ID?

  • 写回答

1条回答 默认 最新

  • duan3019 2012-04-10 11:43
    关注

    Any function mapping [0..N] to [1..9] will have collisions if N is larger than 9 and therefore there does not exist a scheme that maps them uniquely.

    What you can do in your case is to just go through the userIDs and assign them the next free number if you have not seen this userID before. This is a simple for-loop + an array.

    Pseudocode:

    colors = array()
    for userid in userids_to_color:
        if userid not in colors:
            colors[count(colors)] = userid
    

    Colors is then a mapping index (=color number) to userid. You can break out of the loop if count(colors) becomes greater than your maximal number of colors.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭